Dynamically allocate the logcat server vsock port

This is a step closer to a cuttlefish_config.json file that can be
shared between instances.

Bug: 145247175
Test: Build and run locally
Change-Id: Iefccbb82280464cf15001e542db417964e9afc97
diff --git a/host/commands/assemble_cvd/flags.cc b/host/commands/assemble_cvd/flags.cc
index 949a389..ec280e5 100644
--- a/host/commands/assemble_cvd/flags.cc
+++ b/host/commands/assemble_cvd/flags.cc
@@ -164,8 +164,6 @@
               "Binary for the logcat server");
 DEFINE_string(logcat_mode, "", "How to send android's log messages from "
                                "guest to host. One of [serial, vsock]");
-DEFINE_int32(logcat_vsock_port, vsoc::GetPerInstanceDefault(5620),
-             "The port for logcat over vsock");
 DEFINE_string(config_server_binary,
               vsoc::DefaultHostArtifactsPath("bin/config_server"),
               "Binary for the configuration server");
@@ -388,7 +386,6 @@
   }
 
   tmp_config_obj.set_logcat_mode(FLAGS_logcat_mode);
-  tmp_config_obj.set_logcat_vsock_port(FLAGS_logcat_vsock_port);
 
   tmp_config_obj.set_enable_tombstone_receiver(FLAGS_enable_tombstone_receiver);
   tmp_config_obj.set_tombstone_receiver_binary(FLAGS_tombstone_receiver_binary);
diff --git a/host/commands/logcat_receiver/main.cpp b/host/commands/logcat_receiver/main.cpp
index e9cbad2..25f95b6 100644
--- a/host/commands/logcat_receiver/main.cpp
+++ b/host/commands/logcat_receiver/main.cpp
@@ -22,8 +22,7 @@
 
 DEFINE_int32(
     server_fd, -1,
-    "File descriptor to an already created vsock server. If negative a new "
-    "server will be created at the port specified on the config file");
+    "File descriptor to an already created vsock server. Must be specified.");
 
 int main(int argc, char** argv) {
   ::android::base::InitLogging(argv, android::base::StderrLogger);
@@ -37,14 +36,8 @@
   CHECK(logcat_file->IsOpen())
       << "Unable to open logcat file: " << logcat_file->StrError();
 
-  cvd::SharedFD server_fd;
-  if (FLAGS_server_fd < 0) {
-    unsigned int port = config->logcat_vsock_port();
-    server_fd = cvd::SharedFD::VsockServer(port, SOCK_STREAM);
-  } else {
-    server_fd = cvd::SharedFD::Dup(FLAGS_server_fd);
-    close(FLAGS_server_fd);
-  }
+  cvd::SharedFD server_fd = cvd::SharedFD::Dup(FLAGS_server_fd);
+  close(FLAGS_server_fd);
 
   CHECK(server_fd->IsOpen()) << "Error creating or inheriting logcat server: "
                              << server_fd->StrError();
@@ -70,4 +63,4 @@
     }
   }
   return 0;
-}
\ No newline at end of file
+}
diff --git a/host/commands/run_cvd/kernel_args.cc b/host/commands/run_cvd/kernel_args.cc
index f5487d7..3f34c95 100644
--- a/host/commands/run_cvd/kernel_args.cc
+++ b/host/commands/run_cvd/kernel_args.cc
@@ -47,7 +47,6 @@
   kernel_cmdline.push_back(concat("androidboot.serialno=", config.serial_number()));
   kernel_cmdline.push_back(concat("androidboot.lcd_density=", config.dpi()));
   if (config.logcat_mode() == cvd::kLogcatVsockMode) {
-    kernel_cmdline.push_back(concat("androidboot.vsock_logcat_port=", config.logcat_vsock_port()));
   }
   kernel_cmdline.push_back(concat(
       "androidboot.setupwizard_mode=", config.setupwizard_mode()));
@@ -113,3 +112,12 @@
     concat("androidboot.cuttlefish_config_server_port=", *config_server.server_vsock_port),
   };
 }
+
+std::vector<std::string> KernelCommandLineFromLogcatServer(const LogcatServerPorts& logcat_server) {
+  if (!logcat_server.server_vsock_port) {
+    return {};
+  }
+  return {
+    concat("androidboot.vsock_logcat_port=", *logcat_server.server_vsock_port),
+  };
+}
diff --git a/host/commands/run_cvd/kernel_args.h b/host/commands/run_cvd/kernel_args.h
index 1b47a59..bce02ed 100644
--- a/host/commands/run_cvd/kernel_args.h
+++ b/host/commands/run_cvd/kernel_args.h
@@ -26,3 +26,4 @@
 std::vector<std::string> KernelCommandLineFromVnc(const VncServerPorts& vnc_config);
 std::vector<std::string> KernelCommandLineFromTombstone(const TombstoneReceiverPorts& tombstone);
 std::vector<std::string> KernelCommandLineFromConfigServer(const ConfigServerPorts& config_server);
+std::vector<std::string> KernelCommandLineFromLogcatServer(const LogcatServerPorts& config_server);
diff --git a/host/commands/run_cvd/launch.cc b/host/commands/run_cvd/launch.cc
index cb64835..227984d 100644
--- a/host/commands/run_cvd/launch.cc
+++ b/host/commands/run_cvd/launch.cc
@@ -116,13 +116,12 @@
   return ret;
 }
 
-void LaunchLogcatReceiverIfEnabled(const vsoc::CuttlefishConfig& config,
-                                   cvd::ProcessMonitor* process_monitor) {
+LogcatServerPorts LaunchLogcatReceiverIfEnabled(const vsoc::CuttlefishConfig& config,
+                                                cvd::ProcessMonitor* process_monitor) {
   if (!LogcatReceiverEnabled(config)) {
-    return;
+    return {};
   }
-  auto port = config.logcat_vsock_port();
-  auto socket = cvd::SharedFD::VsockServer(port, SOCK_STREAM);
+  auto socket = cvd::SharedFD::VsockServer(SOCK_STREAM);
   if (!socket->IsOpen()) {
     LOG(ERROR) << "Unable to create logcat server socket: "
                << socket->StrError();
@@ -132,6 +131,7 @@
   cmd.AddParameter("-server_fd=", socket);
   process_monitor->StartSubprocess(std::move(cmd),
                                    GetOnSubprocessExitCallback(config));
+  return { socket->VsockServerPort() };
 }
 
 ConfigServerPorts LaunchConfigServer(const vsoc::CuttlefishConfig& config,
diff --git a/host/commands/run_cvd/launch.h b/host/commands/run_cvd/launch.h
index 8a16c3d..efd4966 100644
--- a/host/commands/run_cvd/launch.h
+++ b/host/commands/run_cvd/launch.h
@@ -13,8 +13,6 @@
     const vsoc::CuttlefishConfig& config,
     cvd::ProcessMonitor* process_monitor,
     unsigned int number_of_event_pipes);
-void LaunchLogcatReceiverIfEnabled(const vsoc::CuttlefishConfig& config,
-                                   cvd::ProcessMonitor* process_monitor);
 void LaunchUsbServerIfEnabled(const vsoc::CuttlefishConfig& config,
                               cvd::ProcessMonitor* process_monitor);
 void LaunchAdbConnectorIfEnabled(cvd::ProcessMonitor* process_monitor,
@@ -44,3 +42,9 @@
 };
 ConfigServerPorts LaunchConfigServer(const vsoc::CuttlefishConfig& config,
                                      cvd::ProcessMonitor* process_monitor);
+
+struct LogcatServerPorts {
+  std::optional<int> server_vsock_port;
+};
+LogcatServerPorts LaunchLogcatReceiverIfEnabled(const vsoc::CuttlefishConfig& config,
+                                                cvd::ProcessMonitor* process_monitor);
diff --git a/host/commands/run_cvd/main.cc b/host/commands/run_cvd/main.cc
index caaa1a3..c4972bc 100644
--- a/host/commands/run_cvd/main.cc
+++ b/host/commands/run_cvd/main.cc
@@ -404,7 +404,8 @@
   SetUpHandlingOfBootEvents(&process_monitor, boot_events_pipe,
                             boot_state_machine);
 
-  LaunchLogcatReceiverIfEnabled(*config, &process_monitor);
+  auto logcat_server = LaunchLogcatReceiverIfEnabled(*config, &process_monitor);
+  auto logcat_server_args = KernelCommandLineFromLogcatServer(logcat_server);
 
   auto config_server = LaunchConfigServer(*config, &process_monitor);
   auto config_server_args = KernelCommandLineFromConfigServer(config_server);
@@ -426,6 +427,7 @@
   kernel_args.insert(kernel_args.end(), tombstone_kernel_args.begin(),
                      tombstone_kernel_args.end());
   kernel_args.insert(kernel_args.end(), config_server_args.begin(), config_server_args.end());
+  kernel_args.insert(kernel_args.end(), logcat_server_args.begin(), logcat_server_args.end());
 
   // Start the guest VM
   vm_manager->WithFrontend(vnc_kernel_args.size() > 0);
diff --git a/host/libs/config/cuttlefish_config.cpp b/host/libs/config/cuttlefish_config.cpp
index 370eb0d..2170357 100644
--- a/host/libs/config/cuttlefish_config.cpp
+++ b/host/libs/config/cuttlefish_config.cpp
@@ -141,7 +141,6 @@
 const char* kBlankDataImageFmt = "blank_data_image_fmt";
 
 const char* kLogcatMode = "logcat_mode";
-const char* kLogcatVsockPort = "logcat_vsock_port";
 const char* kLogcatReceiverBinary = "logcat_receiver_binary";
 const char* kConfigServerBinary = "config_server_binary";
 
@@ -696,14 +695,6 @@
   return (*dictionary_)[kLogcatMode].asString();
 }
 
-void CuttlefishConfig::set_logcat_vsock_port(int port) {
-  (*dictionary_)[kLogcatVsockPort] = port;
-}
-
-int CuttlefishConfig::logcat_vsock_port() const {
-  return (*dictionary_)[kLogcatVsockPort].asInt();
-}
-
 void CuttlefishConfig::set_logcat_receiver_binary(const std::string& binary) {
   SetPath(kLogcatReceiverBinary, binary);
 }
diff --git a/host/libs/config/cuttlefish_config.h b/host/libs/config/cuttlefish_config.h
index 36ed090..47a573b 100644
--- a/host/libs/config/cuttlefish_config.h
+++ b/host/libs/config/cuttlefish_config.h
@@ -282,9 +282,6 @@
   void set_logcat_mode(const std::string& mode);
   std::string logcat_mode() const;
 
-  void set_logcat_vsock_port(int port);
-  int logcat_vsock_port() const;
-
   void set_enable_tombstone_receiver(bool enable_tombstone_receiver);
   bool enable_tombstone_receiver() const;