Adds adb_mode flag to use usb or tcp socket
Disabling tcp is done by simply not running the
socket_forward_proxy on the hostside if tcp is disabled.
Disabling usb is done by not setting USBV1SocketName in cfg.
Bug: 77548097
Change-Id: I46d0984553a035eaea8e3c1b0e0de100b1dcfa8c
(cherry picked from commit 6e662ea609b32e698cf3151f2a7a361e3ab6085c)
diff --git a/host/commands/launch/main.cc b/host/commands/launch/main.cc
index 4f5c406..163cb80 100644
--- a/host/commands/launch/main.cc
+++ b/host/commands/launch/main.cc
@@ -122,7 +122,10 @@
StringFromEnv("ANDROID_HOST_OUT", StringFromEnv("HOME", ".")) +
"/bin/socket_forward_proxy",
"Location of the socket_forward_proxy binary.");
-
+DEFINE_string(adb_mode, "tunnel",
+ "Mode for adb connection. Can be usb for usb forwarding, or "
+ "tunnel for tcp connection. If using tunnel, you may have to "
+ "run 'adb kill-server' to get the device to show up.");
DEFINE_bool(start_wifi_relay, true, "Whether to start the wifi_relay process.");
DEFINE_string(wifi_relay_binary,
StringFromEnv("ANDROID_HOST_OUT", StringFromEnv("HOME", ".")) +
@@ -136,6 +139,9 @@
const std::string kDataPolicyCreateIfMissing = "create_if_missing";
const std::string kDataPolicyAlwaysCreate = "always_create";
+constexpr char kAdbModeTunnel[] = "tunnel";
+constexpr char kAdbModeUsb[] = "usb";
+
std::string GetVirshOptions() {
return std::string("-c ").append(FLAGS_hypervisor_uri);
}
@@ -306,7 +312,6 @@
subprocess(rm_command, NULL);
}
-
// Emulators are discovered on odd numbered ports from 5555 to 5585
constexpr int kFirstEmulatorPort = 5555;
@@ -319,11 +324,38 @@
std::to_string(kFirstEmulatorPort + (vsoc::GetDefaultInstance() - 1) * 2);
}
+void ValidateAdbModeFlag() {
+ CHECK(FLAGS_adb_mode == kAdbModeUsb ||
+ FLAGS_adb_mode == kAdbModeTunnel) << "invalid --adb_mode";
+}
+
+bool AdbTunnelEnabled() {
+ return FLAGS_adb_mode == kAdbModeTunnel;
+}
+
+bool AdbUsbEnabled() {
+ return FLAGS_adb_mode == kAdbModeUsb;
+}
+
+void LaunchSocketForwardProxyIfEnabled() {
+ if (AdbTunnelEnabled()) {
+ auto guest_port_arg = GetGuestPortArg();
+ auto host_port_arg = GetHostPortArg();
+
+ const char* const socket_proxy[] =
+ {FLAGS_socket_forward_proxy_binary.c_str(),
+ guest_port_arg.c_str(),
+ host_port_arg.c_str(),
+ NULL};
+ subprocess(socket_proxy, nullptr, false);
+ }
+}
} // anonymous namespace
int main(int argc, char** argv) {
::android::base::InitLogging(argv, android::base::StderrLogger);
google::ParseCommandLineFlags(&argc, &argv, true);
+ ValidateAdbModeFlag();
LOG_IF(FATAL, FLAGS_system_image_dir.empty())
<< "--system_image_dir must be specified.";
@@ -429,15 +461,7 @@
std::string entropy_source = "/dev/urandom";
- auto guest_port_arg = GetGuestPortArg();
- auto host_port_arg = GetHostPortArg();
-
- const char* const socket_proxy[] =
- {FLAGS_socket_forward_proxy_binary.c_str(),
- guest_port_arg.c_str(),
- host_port_arg.c_str(),
- NULL};
- subprocess(socket_proxy, nullptr, false);
+ LaunchSocketForwardProxyIfEnabled();
config::GuestConfig cfg;
cfg.SetID(FLAGS_instance)
@@ -457,8 +481,10 @@
.SetDisableDACSecurity(FLAGS_disable_dac_security)
.SetDisableAppArmorSecurity(FLAGS_disable_app_armor_security)
.SetUUID(FLAGS_uuid);
- cfg.SetUSBV1SocketName(
- GetDefaultPerInstancePath(cfg.GetInstanceName() + "-usb"));
+ if(AdbUsbEnabled()) {
+ cfg.SetUSBV1SocketName(
+ GetDefaultPerInstancePath(cfg.GetInstanceName() + "-usb"));
+ }
cfg.SetKernelLogSocketName(
GetDefaultPerInstancePath(cfg.GetInstanceName() + "-kernel-log"));
diff --git a/host/libs/config/guest_config.cpp b/host/libs/config/guest_config.cpp
index 58cdc70..b1b4d08 100644
--- a/host/libs/config/guest_config.cpp
+++ b/host/libs/config/guest_config.cpp
@@ -210,6 +210,7 @@
// This section adds <channel> elements to <devices> node.
void ConfigureVirtioChannel(xmlNode* devices, int port, const std::string& name,
DeviceSourceType type, const std::string& path) {
+ if (path.empty()) { return; }
auto vch = xmlNewChild(devices, nullptr, xc("channel"), nullptr);
ConfigureDeviceSource(vch, type, path);