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
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/file.h>
|
2016-01-21 17:40:59 +01:00
|
|
|
#include <android-base/parsenetaddress.h>
|
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
|
|
|
|
2015-02-25 00:51:19 +01:00
|
|
|
#if !ADB_HOST
|
|
|
|
#include "cutils/android_reboot.h"
|
|
|
|
#include "cutils/properties.h"
|
|
|
|
#endif
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
#include "adb.h"
|
2015-02-25 06:26:58 +01:00
|
|
|
#include "adb_io.h"
|
2015-07-18 21:21:30 +02:00
|
|
|
#include "adb_utils.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
#include "file_sync_service.h"
|
2015-03-16 22:58:32 +01:00
|
|
|
#include "remount_service.h"
|
2015-09-30 22:35:42 +02:00
|
|
|
#include "services.h"
|
2015-08-29 00:08:49 +02:00
|
|
|
#include "shell_service.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
|
|
|
|
|
|
|
struct stinfo {
|
|
|
|
void (*func)(int fd, void *cookie);
|
|
|
|
int fd;
|
|
|
|
void *cookie;
|
|
|
|
};
|
|
|
|
|
2016-02-12 23:31:15 +01:00
|
|
|
static void service_bootstrap_func(void* x) {
|
2015-02-26 02:51:28 +01:00
|
|
|
stinfo* sti = reinterpret_cast<stinfo*>(x);
|
2015-08-29 01:37:29 +02:00
|
|
|
adb_thread_setname(android::base::StringPrintf("service %d", sti->fd));
|
2009-03-04 04:32:55 +01:00
|
|
|
sti->func(sti->fd, sti->cookie);
|
|
|
|
free(sti);
|
|
|
|
}
|
|
|
|
|
2013-02-21 00:04:53 +01:00
|
|
|
#if !ADB_HOST
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-05-02 02:04:38 +02:00
|
|
|
void restart_root_service(int fd, void *cookie) {
|
2009-03-13 21:04:37 +01:00
|
|
|
if (getuid() == 0) {
|
2015-05-02 02:04:38 +02:00
|
|
|
WriteFdExactly(fd, "adbd is already running as root\n");
|
2009-03-13 21:04:37 +01:00
|
|
|
adb_close(fd);
|
|
|
|
} else {
|
2015-05-02 02:04:38 +02:00
|
|
|
char value[PROPERTY_VALUE_MAX];
|
2009-03-13 21:04:37 +01:00
|
|
|
property_get("ro.debuggable", value, "");
|
|
|
|
if (strcmp(value, "1") != 0) {
|
2015-05-02 02:04:38 +02:00
|
|
|
WriteFdExactly(fd, "adbd cannot run as root in production builds\n");
|
2009-08-25 00:58:40 +02:00
|
|
|
adb_close(fd);
|
2009-03-13 21:04:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-16 22:44:17 +01:00
|
|
|
property_set("service.adb.root", "1");
|
2015-05-02 02:04:38 +02:00
|
|
|
WriteFdExactly(fd, "restarting adbd as root\n");
|
2009-03-13 21:04:37 +01:00
|
|
|
adb_close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-02 02:04:38 +02:00
|
|
|
void restart_unroot_service(int fd, void *cookie) {
|
2014-10-06 19:57:20 +02:00
|
|
|
if (getuid() != 0) {
|
2015-05-02 02:04:38 +02:00
|
|
|
WriteFdExactly(fd, "adbd not running as root\n");
|
2014-10-06 19:57:20 +02:00
|
|
|
adb_close(fd);
|
|
|
|
} else {
|
|
|
|
property_set("service.adb.root", "0");
|
2015-05-02 02:04:38 +02:00
|
|
|
WriteFdExactly(fd, "restarting adbd as non root\n");
|
2014-10-06 19:57:20 +02:00
|
|
|
adb_close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-02 02:04:38 +02:00
|
|
|
void restart_tcp_service(int fd, void *cookie) {
|
2014-01-16 19:53:11 +01:00
|
|
|
int port = (int) (uintptr_t) cookie;
|
2009-08-25 00:58:40 +02:00
|
|
|
if (port <= 0) {
|
2015-05-02 02:04:38 +02:00
|
|
|
WriteFdFmt(fd, "invalid port %d\n", port);
|
2009-08-25 00:58:40 +02:00
|
|
|
adb_close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-02 02:04:38 +02:00
|
|
|
char value[PROPERTY_VALUE_MAX];
|
2009-08-25 00:58:40 +02:00
|
|
|
snprintf(value, sizeof(value), "%d", port);
|
|
|
|
property_set("service.adb.tcp.port", value);
|
2015-05-02 02:04:38 +02:00
|
|
|
WriteFdFmt(fd, "restarting in TCP mode port: %d\n", port);
|
2009-08-25 00:58:40 +02:00
|
|
|
adb_close(fd);
|
|
|
|
}
|
|
|
|
|
2015-05-02 02:04:38 +02:00
|
|
|
void restart_usb_service(int fd, void *cookie) {
|
2009-08-25 00:58:40 +02:00
|
|
|
property_set("service.adb.tcp.port", "0");
|
2015-05-02 02:04:38 +02:00
|
|
|
WriteFdExactly(fd, "restarting in USB mode\n");
|
2009-08-25 00:58:40 +02:00
|
|
|
adb_close(fd);
|
|
|
|
}
|
|
|
|
|
2015-03-29 20:22:34 +02:00
|
|
|
static bool reboot_service_impl(int fd, const char* arg) {
|
|
|
|
const char* reboot_arg = arg;
|
|
|
|
bool auto_reboot = false;
|
|
|
|
|
|
|
|
if (strcmp(reboot_arg, "sideload-auto-reboot") == 0) {
|
|
|
|
auto_reboot = true;
|
|
|
|
reboot_arg = "sideload";
|
|
|
|
}
|
|
|
|
|
|
|
|
// It reboots into sideload mode by setting "--sideload" or "--sideload_auto_reboot"
|
|
|
|
// in the command file.
|
|
|
|
if (strcmp(reboot_arg, "sideload") == 0) {
|
|
|
|
if (getuid() != 0) {
|
2015-05-01 02:32:03 +02:00
|
|
|
WriteFdExactly(fd, "'adb root' is required for 'adb reboot sideload'.\n");
|
2015-03-29 20:22:34 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* const recovery_dir = "/cache/recovery";
|
|
|
|
const char* const command_file = "/cache/recovery/command";
|
|
|
|
// Ensure /cache/recovery exists.
|
|
|
|
if (adb_mkdir(recovery_dir, 0770) == -1 && errno != EEXIST) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("Failed to create directory '%s': %s", recovery_dir, strerror(errno));
|
2015-03-29 20:22:34 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool write_status = android::base::WriteStringToFile(
|
|
|
|
auto_reboot ? "--sideload_auto_reboot" : "--sideload", command_file);
|
|
|
|
if (!write_status) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
reboot_arg = "recovery";
|
|
|
|
}
|
2009-08-05 02:37:51 +02:00
|
|
|
|
|
|
|
sync();
|
2010-02-24 22:07:23 +01:00
|
|
|
|
2015-03-29 20:22:34 +02:00
|
|
|
char property_val[PROPERTY_VALUE_MAX];
|
|
|
|
int ret = snprintf(property_val, sizeof(property_val), "reboot,%s", reboot_arg);
|
|
|
|
if (ret >= static_cast<int>(sizeof(property_val))) {
|
2015-05-02 02:04:38 +02:00
|
|
|
WriteFdFmt(fd, "reboot string too long: %d\n", ret);
|
2015-03-29 20:22:34 +02:00
|
|
|
return false;
|
2013-04-18 21:20:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = property_set(ANDROID_RB_PROPERTY, property_val);
|
2009-08-05 02:37:51 +02:00
|
|
|
if (ret < 0) {
|
2015-05-02 02:04:38 +02:00
|
|
|
WriteFdFmt(fd, "reboot failed: %d\n", ret);
|
2015-03-29 20:22:34 +02:00
|
|
|
return false;
|
2009-08-05 02:37:51 +02:00
|
|
|
}
|
2015-03-29 20:22:34 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reboot_service(int fd, void* arg)
|
|
|
|
{
|
|
|
|
if (reboot_service_impl(fd, static_cast<const char*>(arg))) {
|
|
|
|
// Don't return early. Give the reboot command time to take effect
|
|
|
|
// to avoid messing up scripts which do "adb reboot && adb wait-for-device"
|
2015-04-18 02:03:59 +02:00
|
|
|
while (true) {
|
2015-03-29 20:22:34 +02:00
|
|
|
pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-19 22:52:58 +02:00
|
|
|
free(arg);
|
2009-08-05 02:37:51 +02:00
|
|
|
adb_close(fd);
|
|
|
|
}
|
|
|
|
|
2015-10-31 02:37:26 +01:00
|
|
|
int reverse_service(const char* command) {
|
|
|
|
int s[2];
|
|
|
|
if (adb_socketpair(s)) {
|
|
|
|
PLOG(ERROR) << "cannot create service socket pair.";
|
|
|
|
return -1;
|
2013-03-21 21:07:42 +01:00
|
|
|
}
|
2015-10-31 02:37:26 +01:00
|
|
|
VLOG(SERVICES) << "service socketpair: " << s[0] << ", " << s[1];
|
|
|
|
if (handle_forward_request(command, kTransportAny, nullptr, s[1]) < 0) {
|
|
|
|
SendFail(s[1], "not a reverse forwarding command");
|
|
|
|
}
|
|
|
|
adb_close(s[1]);
|
|
|
|
return s[0];
|
2013-03-21 21:07:42 +01:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:43:08 +02:00
|
|
|
// Shell service string can look like:
|
2015-09-30 22:35:42 +02:00
|
|
|
// shell[,arg1,arg2,...]:[command]
|
2015-09-22 19:43:08 +02:00
|
|
|
static int ShellService(const std::string& args, const atransport* transport) {
|
|
|
|
size_t delimiter_index = args.find(':');
|
|
|
|
if (delimiter_index == std::string::npos) {
|
|
|
|
LOG(ERROR) << "No ':' found in shell service arguments: " << args;
|
|
|
|
return -1;
|
|
|
|
}
|
2015-09-30 22:35:42 +02:00
|
|
|
|
2015-09-22 19:43:08 +02:00
|
|
|
const std::string service_args = args.substr(0, delimiter_index);
|
|
|
|
const std::string command = args.substr(delimiter_index + 1);
|
|
|
|
|
2015-09-30 22:35:42 +02:00
|
|
|
// Defaults:
|
|
|
|
// PTY for interactive, raw for non-interactive.
|
|
|
|
// No protocol.
|
2015-11-16 19:55:34 +01:00
|
|
|
// $TERM set to "dumb".
|
2015-09-30 22:35:42 +02:00
|
|
|
SubprocessType type(command.empty() ? SubprocessType::kPty
|
|
|
|
: SubprocessType::kRaw);
|
|
|
|
SubprocessProtocol protocol = SubprocessProtocol::kNone;
|
2015-11-16 19:55:34 +01:00
|
|
|
std::string terminal_type = "dumb";
|
2015-09-30 22:35:42 +02:00
|
|
|
|
|
|
|
for (const std::string& arg : android::base::Split(service_args, ",")) {
|
|
|
|
if (arg == kShellServiceArgRaw) {
|
|
|
|
type = SubprocessType::kRaw;
|
|
|
|
} else if (arg == kShellServiceArgPty) {
|
|
|
|
type = SubprocessType::kPty;
|
|
|
|
} else if (arg == kShellServiceArgShellProtocol) {
|
|
|
|
protocol = SubprocessProtocol::kShell;
|
2015-11-16 19:55:34 +01:00
|
|
|
} else if (android::base::StartsWith(arg, "TERM=")) {
|
|
|
|
terminal_type = arg.substr(5);
|
|
|
|
} else if (!arg.empty()) {
|
|
|
|
// This is not an error to allow for future expansion.
|
|
|
|
LOG(WARNING) << "Ignoring unknown shell service argument: " << arg;
|
2015-09-30 22:35:42 +02:00
|
|
|
}
|
2015-09-22 19:43:08 +02:00
|
|
|
}
|
|
|
|
|
2015-11-16 19:55:34 +01:00
|
|
|
return StartSubprocess(command.c_str(), terminal_type.c_str(), type, protocol);
|
2015-09-22 19:43:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !ADB_HOST
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
static int create_service_thread(void (*func)(int, void *), void *cookie)
|
|
|
|
{
|
|
|
|
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");
|
|
|
|
return -1;
|
|
|
|
}
|
2015-09-03 02:44:28 +02:00
|
|
|
D("socketpair: (%d,%d)", s[0], s[1]);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
|
|
|
|
if (sti == nullptr) {
|
|
|
|
fatal("cannot allocate stinfo");
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
sti->func = func;
|
|
|
|
sti->cookie = cookie;
|
|
|
|
sti->fd = s[1];
|
|
|
|
|
2015-05-05 22:41:21 +02:00
|
|
|
if (!adb_thread_create(service_bootstrap_func, sti)) {
|
2009-03-04 04:32:55 +01:00
|
|
|
free(sti);
|
|
|
|
adb_close(s[0]);
|
|
|
|
adb_close(s[1]);
|
|
|
|
printf("cannot create service thread\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("service thread started, %d:%d",s[0], s[1]);
|
2009-03-04 04:32:55 +01:00
|
|
|
return s[0];
|
|
|
|
}
|
|
|
|
|
2015-08-31 19:42:13 +02:00
|
|
|
int service_to_fd(const char* name, const atransport* transport) {
|
2009-03-04 04:32:55 +01:00
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if(!strncmp(name, "tcp:", 4)) {
|
|
|
|
int port = atoi(name + 4);
|
|
|
|
name = strchr(name + 4, ':');
|
|
|
|
if(name == 0) {
|
2015-07-31 08:07:55 +02:00
|
|
|
std::string error;
|
|
|
|
ret = network_loopback_client(port, SOCK_STREAM, &error);
|
2009-03-04 04:32:55 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
disable_tcp_nagle(ret);
|
|
|
|
} else {
|
|
|
|
#if ADB_HOST
|
2015-07-24 02:12:58 +02:00
|
|
|
std::string error;
|
|
|
|
ret = network_connect(name + 1, port, SOCK_STREAM, 0, &error);
|
2009-03-04 04:32:55 +01:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
2015-07-30 02:45:24 +02:00
|
|
|
#if !defined(_WIN32) /* winsock doesn't implement unix domain sockets */
|
2009-03-04 04:32:55 +01:00
|
|
|
} else if(!strncmp(name, "local:", 6)) {
|
|
|
|
ret = socket_local_client(name + 6,
|
|
|
|
ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
|
|
|
|
} else if(!strncmp(name, "localreserved:", 14)) {
|
|
|
|
ret = socket_local_client(name + 14,
|
|
|
|
ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
|
|
|
|
} else if(!strncmp(name, "localabstract:", 14)) {
|
|
|
|
ret = socket_local_client(name + 14,
|
|
|
|
ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
|
|
|
|
} else if(!strncmp(name, "localfilesystem:", 16)) {
|
|
|
|
ret = socket_local_client(name + 16,
|
|
|
|
ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
|
|
|
|
#endif
|
2013-02-21 00:04:53 +01:00
|
|
|
#if !ADB_HOST
|
2009-03-04 04:32:55 +01:00
|
|
|
} else if(!strncmp("dev:", name, 4)) {
|
2014-07-19 05:57:35 +02:00
|
|
|
ret = unix_open(name + 4, O_RDWR | O_CLOEXEC);
|
2009-03-04 04:32:55 +01:00
|
|
|
} else if(!strncmp(name, "framebuffer:", 12)) {
|
|
|
|
ret = create_service_thread(framebuffer_service, 0);
|
|
|
|
} else if (!strncmp(name, "jdwp:", 5)) {
|
|
|
|
ret = create_jdwp_connection_fd(atoi(name+5));
|
2015-09-22 19:43:08 +02:00
|
|
|
} else if(!strncmp(name, "shell", 5)) {
|
|
|
|
ret = ShellService(name + 5, transport);
|
2015-08-11 22:40:42 +02:00
|
|
|
} else if(!strncmp(name, "exec:", 5)) {
|
2015-11-16 19:55:34 +01:00
|
|
|
ret = StartSubprocess(name + 5, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
|
2009-03-04 04:32:55 +01:00
|
|
|
} else if(!strncmp(name, "sync:", 5)) {
|
|
|
|
ret = create_service_thread(file_sync_service, NULL);
|
|
|
|
} else if(!strncmp(name, "remount:", 8)) {
|
|
|
|
ret = create_service_thread(remount_service, NULL);
|
2009-08-05 02:37:51 +02:00
|
|
|
} else if(!strncmp(name, "reboot:", 7)) {
|
2009-09-19 22:52:58 +02:00
|
|
|
void* arg = strdup(name + 7);
|
2014-05-27 03:30:43 +02:00
|
|
|
if (arg == NULL) return -1;
|
2009-09-19 22:52:58 +02:00
|
|
|
ret = create_service_thread(reboot_service, arg);
|
2009-03-13 21:04:37 +01:00
|
|
|
} else if(!strncmp(name, "root:", 5)) {
|
|
|
|
ret = create_service_thread(restart_root_service, NULL);
|
2014-10-06 19:57:20 +02:00
|
|
|
} else if(!strncmp(name, "unroot:", 7)) {
|
|
|
|
ret = create_service_thread(restart_unroot_service, NULL);
|
2011-04-21 21:53:28 +02:00
|
|
|
} else if(!strncmp(name, "backup:", 7)) {
|
2015-08-29 00:08:49 +02:00
|
|
|
ret = StartSubprocess(android::base::StringPrintf("/system/bin/bu backup %s",
|
|
|
|
(name + 7)).c_str(),
|
2015-11-16 19:55:34 +01:00
|
|
|
nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
|
2015-08-03 19:38:08 +02:00
|
|
|
} else if(!strncmp(name, "restore:", 8)) {
|
2015-11-16 19:55:34 +01:00
|
|
|
ret = StartSubprocess("/system/bin/bu restore", nullptr, SubprocessType::kRaw,
|
2015-08-31 19:42:13 +02:00
|
|
|
SubprocessProtocol::kNone);
|
2009-08-25 00:58:40 +02:00
|
|
|
} else if(!strncmp(name, "tcpip:", 6)) {
|
|
|
|
int port;
|
2015-01-26 02:38:36 +01:00
|
|
|
if (sscanf(name + 6, "%d", &port) != 1) {
|
2015-07-22 01:13:40 +02:00
|
|
|
return -1;
|
2009-08-25 00:58:40 +02:00
|
|
|
}
|
2014-01-16 19:53:11 +01:00
|
|
|
ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
|
2009-08-25 00:58:40 +02:00
|
|
|
} else if(!strncmp(name, "usb:", 4)) {
|
|
|
|
ret = create_service_thread(restart_usb_service, NULL);
|
2013-03-21 21:07:42 +01:00
|
|
|
} else if (!strncmp(name, "reverse:", 8)) {
|
2015-10-31 02:37:26 +01:00
|
|
|
ret = reverse_service(name + 8);
|
2014-10-09 16:22:49 +02:00
|
|
|
} else if(!strncmp(name, "disable-verity:", 15)) {
|
2014-12-04 00:31:57 +01:00
|
|
|
ret = create_service_thread(set_verity_enabled_state_service, (void*)0);
|
|
|
|
} else if(!strncmp(name, "enable-verity:", 15)) {
|
|
|
|
ret = create_service_thread(set_verity_enabled_state_service, (void*)1);
|
2009-03-04 04:32:55 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (ret >= 0) {
|
|
|
|
close_on_exec(ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#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;
|
2015-05-19 01:43:57 +02:00
|
|
|
ConnectionState state;
|
2009-03-04 04:32:55 +01:00
|
|
|
};
|
|
|
|
|
2015-11-27 18:56:48 +01:00
|
|
|
static void wait_for_state(int fd, void* data) {
|
|
|
|
std::unique_ptr<state_info> sinfo(reinterpret_cast<state_info*>(data));
|
2009-03-04 04:32:55 +01:00
|
|
|
|
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";
|
2016-01-05 12:06:05 +01:00
|
|
|
const char* serial = sinfo->serial.length() ? sinfo->serial.c_str() : NULL;
|
|
|
|
atransport* t = acquire_one_transport(sinfo->transport_type, serial, &is_ambiguous, &error);
|
2015-10-07 23:55:10 +02:00
|
|
|
if (t != nullptr && t->connection_state == sinfo->state) {
|
|
|
|
SendOkay(fd);
|
|
|
|
break;
|
|
|
|
} else if (!is_ambiguous) {
|
2016-02-19 19:42:40 +01:00
|
|
|
adb_pollfd pfd = {.fd = fd, .events = POLLIN };
|
|
|
|
int rc = adb_poll(&pfd, 1, 1000);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
adb_close(fd);
|
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-07-18 21:21:30 +02:00
|
|
|
static void connect_device(const std::string& address, std::string* response) {
|
|
|
|
if (address.empty()) {
|
|
|
|
*response = "empty address";
|
2015-05-01 02:32:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-18 21:21:30 +02:00
|
|
|
std::string serial;
|
|
|
|
std::string host;
|
2015-05-01 02:32:03 +02:00
|
|
|
int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
|
2016-01-21 17:40:59 +01:00
|
|
|
if (!android::base::ParseNetAddress(address, &host, &port, &serial, response)) {
|
2015-07-18 21:21:30 +02:00
|
|
|
return;
|
2013-03-30 02:22:36 +01:00
|
|
|
}
|
|
|
|
|
2015-07-24 02:12:58 +02:00
|
|
|
std::string error;
|
|
|
|
int fd = network_connect(host.c_str(), port, SOCK_STREAM, 10, &error);
|
2015-07-18 21:21:30 +02:00
|
|
|
if (fd == -1) {
|
|
|
|
*response = android::base::StringPrintf("unable to connect to %s: %s",
|
2015-07-24 02:12:58 +02:00
|
|
|
serial.c_str(), error.c_str());
|
2013-03-30 02:22:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("client: connected %s remote on fd %d", serial.c_str(), fd);
|
2013-03-30 02:22:36 +01:00
|
|
|
close_on_exec(fd);
|
|
|
|
disable_tcp_nagle(fd);
|
|
|
|
|
2016-02-22 23:27:23 +01:00
|
|
|
// Send a TCP keepalive ping to the device every second so we can detect disconnects.
|
|
|
|
if (!set_tcp_keepalive(fd, 1)) {
|
|
|
|
D("warning: failed to configure TCP keepalives (%s)", strerror(errno));
|
|
|
|
}
|
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
int ret = register_socket_transport(fd, serial.c_str(), port, 0);
|
2013-03-30 02:22:36 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
adb_close(fd);
|
2015-05-01 02:32:03 +02:00
|
|
|
*response = android::base::StringPrintf("already connected to %s", serial.c_str());
|
2013-03-30 02:22:36 +01:00
|
|
|
} else {
|
2015-05-01 02:32:03 +02:00
|
|
|
*response = android::base::StringPrintf("connected to %s", serial.c_str());
|
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;
|
|
|
|
}
|
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
int console_port = strtol(pieces[0].c_str(), NULL, 0);
|
|
|
|
int adb_port = strtol(pieces[1].c_str(), NULL, 0);
|
|
|
|
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
|
|
|
// Check if more emulators can be registered. Similar unproblematic
|
|
|
|
// race condition as above.
|
2013-03-30 02:22:36 +01:00
|
|
|
int candidate_slot = get_available_local_transport_index();
|
|
|
|
if (candidate_slot < 0) {
|
2015-05-01 02:32:03 +02:00
|
|
|
*response = "Cannot accept more emulators";
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-16 10:05:25 +02:00
|
|
|
static void connect_service(int fd, void* data) {
|
|
|
|
char* host = reinterpret_cast<char*>(data);
|
2015-05-01 02:32:03 +02:00
|
|
|
std::string response;
|
2013-03-30 02:22:36 +01:00
|
|
|
if (!strncmp(host, "emu:", 4)) {
|
2015-05-01 02:32:03 +02:00
|
|
|
connect_emulator(host + 4, &response);
|
2013-03-30 02:22:36 +01:00
|
|
|
} else {
|
2015-05-01 02:32:03 +02:00
|
|
|
connect_device(host, &response);
|
2013-03-30 02:22:36 +01:00
|
|
|
}
|
2014-10-16 10:05:25 +02:00
|
|
|
free(host);
|
2013-03-30 02:22:36 +01:00
|
|
|
|
|
|
|
// Send response for emulator and device
|
2015-05-01 02:32:03 +02:00
|
|
|
SendProtocolString(fd, response);
|
2013-03-30 02:22:36 +01:00
|
|
|
adb_close(fd);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ADB_HOST
|
2015-08-03 19:38:08 +02:00
|
|
|
asocket* host_service_to_socket(const char* name, const char* serial) {
|
2009-03-04 04:32:55 +01:00
|
|
|
if (!strcmp(name,"track-devices")) {
|
|
|
|
return create_device_tracker();
|
2015-11-27 18:56:48 +01:00
|
|
|
} else if (android::base::StartsWith(name, "wait-for-")) {
|
|
|
|
name += strlen("wait-for-");
|
|
|
|
|
|
|
|
std::unique_ptr<state_info> sinfo(new 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
|
|
|
|
2015-11-27 18:56:48 +01:00
|
|
|
if (serial) sinfo->serial = serial;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-11-27 18:56:48 +01:00
|
|
|
if (android::base::StartsWith(name, "local")) {
|
|
|
|
name += strlen("local");
|
2015-05-05 22:10:43 +02:00
|
|
|
sinfo->transport_type = kTransportLocal;
|
2015-11-27 18:56:48 +01:00
|
|
|
} else if (android::base::StartsWith(name, "usb")) {
|
|
|
|
name += strlen("usb");
|
2015-05-05 22:10:43 +02:00
|
|
|
sinfo->transport_type = kTransportUsb;
|
2015-11-27 18:56:48 +01:00
|
|
|
} else if (android::base::StartsWith(name, "any")) {
|
|
|
|
name += strlen("any");
|
2015-05-05 22:10:43 +02:00
|
|
|
sinfo->transport_type = kTransportAny;
|
2015-11-27 18:56:48 +01:00
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(name, "-device")) {
|
2015-05-19 01:43:57 +02:00
|
|
|
sinfo->state = kCsDevice;
|
2015-11-27 18:56:48 +01:00
|
|
|
} else if (!strcmp(name, "-recovery")) {
|
|
|
|
sinfo->state = kCsRecovery;
|
|
|
|
} else if (!strcmp(name, "-sideload")) {
|
|
|
|
sinfo->state = kCsSideload;
|
|
|
|
} else if (!strcmp(name, "-bootloader")) {
|
|
|
|
sinfo->state = kCsBootloader;
|
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
|
|
|
}
|
|
|
|
|
2015-11-27 18:56:48 +01:00
|
|
|
int fd = create_service_thread(wait_for_state, sinfo.release());
|
2009-03-04 04:32:55 +01:00
|
|
|
return create_local_socket(fd);
|
2013-03-30 02:22:36 +01:00
|
|
|
} else if (!strncmp(name, "connect:", 8)) {
|
2014-10-16 10:05:25 +02:00
|
|
|
char* host = strdup(name + 8);
|
|
|
|
int fd = create_service_thread(connect_service, host);
|
2013-03-30 02:22:36 +01:00
|
|
|
return create_local_socket(fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif /* ADB_HOST */
|