Merge changes I533ff9c1,If06d6f1c

* changes:
  Move the kernel argument generation to another file.
  Dynamically choose the tombstone receiver port.
diff --git a/host/commands/assemble_cvd/flags.cc b/host/commands/assemble_cvd/flags.cc
index 21781bf..02eb5ff 100644
--- a/host/commands/assemble_cvd/flags.cc
+++ b/host/commands/assemble_cvd/flags.cc
@@ -176,8 +176,6 @@
 DEFINE_string(tombstone_receiver_binary,
               vsoc::DefaultHostArtifactsPath("bin/tombstone_receiver"),
               "Binary for the tombstone server");
-DEFINE_int32(tombstone_receiver_port, vsoc::GetPerInstanceDefault(5630),
-             "The vsock port for tombstones");
 DEFINE_bool(use_bootloader, false, "Boots the device using a bootloader");
 DEFINE_string(bootloader, "", "Bootloader binary path");
 DEFINE_string(boot_slot, "", "Force booting into the given slot. If empty, "
@@ -396,7 +394,6 @@
   tmp_config_obj.set_config_server_port(FLAGS_config_server_port);
 
   tmp_config_obj.set_enable_tombstone_receiver(FLAGS_enable_tombstone_receiver);
-  tmp_config_obj.set_tombstone_receiver_port(FLAGS_tombstone_receiver_port);
   tmp_config_obj.set_tombstone_receiver_binary(FLAGS_tombstone_receiver_binary);
 
   tmp_config_obj.set_use_bootloader(FLAGS_use_bootloader);
diff --git a/host/commands/run_cvd/Android.bp b/host/commands/run_cvd/Android.bp
index a7893fe..c338f08 100644
--- a/host/commands/run_cvd/Android.bp
+++ b/host/commands/run_cvd/Android.bp
@@ -16,9 +16,10 @@
 cc_binary_host {
     name: "run_cvd",
     srcs: [
+        "kernel_args.cc",
         "launch.cc",
-        "process_monitor.cc",
         "main.cc",
+        "process_monitor.cc",
     ],
     header_libs: [
         "cuttlefish_glog",
diff --git a/host/commands/run_cvd/kernel_args.cc b/host/commands/run_cvd/kernel_args.cc
new file mode 100644
index 0000000..4d0b35f
--- /dev/null
+++ b/host/commands/run_cvd/kernel_args.cc
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+#include "host/commands/run_cvd/kernel_args.h"
+
+#include <string>
+#include <vector>
+
+#include "host/commands/run_cvd/launch.h"
+#include "host/commands/run_cvd/runner_defs.h"
+#include "host/libs/config/cuttlefish_config.h"
+#include "host/libs/vm_manager/vm_manager.h"
+
+template<typename T>
+static void AppendVector(std::vector<T>* destination, const std::vector<T>& source) {
+  destination->insert(destination->end(), source.begin(), source.end());
+}
+
+template<typename S, typename T>
+static std::string concat(const S& s, const T& t) {
+  std::ostringstream os;
+  os << s << t;
+  return os.str();
+}
+
+std::vector<std::string> KernelCommandLineFromConfig(const vsoc::CuttlefishConfig& config) {
+  std::vector<std::string> kernel_cmdline;
+
+  AppendVector(&kernel_cmdline, config.boot_image_kernel_cmdline());
+  AppendVector(&kernel_cmdline,
+               vm_manager::VmManager::ConfigureGpuMode(config.vm_manager(), config.gpu_mode()));
+  AppendVector(&kernel_cmdline, vm_manager::VmManager::ConfigureBootDevices(config.vm_manager()));
+
+  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.cuttlefish_config_server_port=", config.config_server_port()));
+  kernel_cmdline.push_back(concat(
+      "androidboot.setupwizard_mode=", config.setupwizard_mode()));
+  if (!config.use_bootloader()) {
+    std::string slot_suffix;
+    if (config.boot_slot().empty()) {
+      slot_suffix = "_a";
+    } else {
+      slot_suffix = "_" + config.boot_slot();
+    }
+    kernel_cmdline.push_back(concat("androidboot.slot_suffix=", slot_suffix));
+  }
+  kernel_cmdline.push_back(concat("loop.max_part=", config.loop_max_part()));
+  if (config.guest_enforce_security()) {
+    kernel_cmdline.push_back("enforcing=1");
+  } else {
+    kernel_cmdline.push_back("enforcing=0");
+    kernel_cmdline.push_back("androidboot.selinux=permissive");
+  }
+  if (config.guest_audit_security()) {
+    kernel_cmdline.push_back("audit=1");
+  } else {
+    kernel_cmdline.push_back("audit=0");
+  }
+
+  AppendVector(&kernel_cmdline, config.extra_kernel_cmdline());
+
+  return kernel_cmdline;
+}
+
+std::vector<std::string> KernelCommandLineFromVnc(const VncServerPorts& vnc_ports) {
+  std::vector<std::string> kernel_args;
+  if (vnc_ports.frames_server_vsock_port) {
+    kernel_args.push_back(concat("androidboot.vsock_frames_port=",
+                                 *vnc_ports.frames_server_vsock_port));
+  }
+  if (vnc_ports.touch_server_vsock_port) {
+    kernel_args.push_back(concat("androidboot.vsock_touch_port=",
+                                 *vnc_ports.touch_server_vsock_port));
+  }
+  if (vnc_ports.keyboard_server_vsock_port) {
+    kernel_args.push_back(concat("androidboot.vsock_keyboard_port=",
+                                 *vnc_ports.keyboard_server_vsock_port));
+  }
+  return kernel_args;
+}
+
+std::vector<std::string> KernelCommandLineFromTombstone(const TombstoneReceiverPorts& tombstone) {
+  if (!tombstone.server_vsock_port) {
+    return { "androidboot.tombstone_transmit=0" };
+  }
+  return {
+    "androidboot.tombstone_transmit=1",
+    concat("androidboot.vsock_tombstone_port=", *tombstone.server_vsock_port),
+  };
+}
diff --git a/host/commands/run_cvd/kernel_args.h b/host/commands/run_cvd/kernel_args.h
new file mode 100644
index 0000000..9356023
--- /dev/null
+++ b/host/commands/run_cvd/kernel_args.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "host/commands/run_cvd/launch.h"
+#include "host/libs/config/cuttlefish_config.h"
+
+std::vector<std::string> KernelCommandLineFromConfig(const vsoc::CuttlefishConfig& config);
+std::vector<std::string> KernelCommandLineFromVnc(const VncServerPorts& vnc_config);
+std::vector<std::string> KernelCommandLineFromTombstone(const TombstoneReceiverPorts& tombstone);
diff --git a/host/commands/run_cvd/launch.cc b/host/commands/run_cvd/launch.cc
index 0d8aace..72b504c 100644
--- a/host/commands/run_cvd/launch.cc
+++ b/host/commands/run_cvd/launch.cc
@@ -149,10 +149,10 @@
                                    GetOnSubprocessExitCallback(config));
 }
 
-void LaunchTombstoneReceiverIfEnabled(const vsoc::CuttlefishConfig& config,
-                                      cvd::ProcessMonitor* process_monitor) {
+TombstoneReceiverPorts LaunchTombstoneReceiverIfEnabled(
+    const vsoc::CuttlefishConfig& config, cvd::ProcessMonitor* process_monitor) {
   if (!config.enable_tombstone_receiver()) {
-    return;
+    return {};
   }
 
   std::string tombstoneDir = config.PerInstancePath("tombstones");
@@ -163,15 +163,16 @@
       LOG(ERROR) << "Failed to create tombstone directory: " << tombstoneDir
                  << ". Error: " << errno;
       exit(RunnerExitCodes::kTombstoneDirCreationError);
+      return {};
     }
   }
 
-  auto port = config.tombstone_receiver_port();
-  auto socket = cvd::SharedFD::VsockServer(port, SOCK_STREAM);
+  auto socket = cvd::SharedFD::VsockServer(SOCK_STREAM);
   if (!socket->IsOpen()) {
     LOG(ERROR) << "Unable to create tombstone server socket: "
                << socket->StrError();
     std::exit(RunnerExitCodes::kTombstoneServerError);
+    return {};
   }
   cvd::Command cmd(config.tombstone_receiver_binary());
   cmd.AddParameter("-server_fd=", socket);
@@ -179,6 +180,7 @@
 
   process_monitor->StartSubprocess(std::move(cmd),
                                    GetOnSubprocessExitCallback(config));
+  return { socket->VsockServerPort() };
 }
 
 void LaunchUsbServerIfEnabled(const vsoc::CuttlefishConfig& config,
diff --git a/host/commands/run_cvd/launch.h b/host/commands/run_cvd/launch.h
index 42eed0d..f35b372 100644
--- a/host/commands/run_cvd/launch.h
+++ b/host/commands/run_cvd/launch.h
@@ -24,8 +24,6 @@
                                  cvd::SharedFD adbd_events_pipe);
 void LaunchSocketVsockProxyIfEnabled(cvd::ProcessMonitor* process_monitor,
                                  const vsoc::CuttlefishConfig& config);
-void LaunchTombstoneReceiverIfEnabled(const vsoc::CuttlefishConfig& config,
-                                      cvd::ProcessMonitor* process_monitor);
 
 struct VncServerPorts {
   std::optional<int> frames_server_vsock_port;
@@ -36,3 +34,9 @@
     const vsoc::CuttlefishConfig& config,
     cvd::ProcessMonitor* process_monitor,
     std::function<bool(cvd::MonitorEntry*)> callback);
+
+struct TombstoneReceiverPorts {
+  std::optional<int> server_vsock_port;
+};
+TombstoneReceiverPorts LaunchTombstoneReceiverIfEnabled(
+    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 470c0d5..7bc2c55 100644
--- a/host/commands/run_cvd/main.cc
+++ b/host/commands/run_cvd/main.cc
@@ -46,6 +46,7 @@
 #include "common/libs/utils/files.h"
 #include "common/libs/utils/subprocess.h"
 #include "common/libs/utils/size_utils.h"
+#include "host/commands/run_cvd/kernel_args.h"
 #include "host/commands/run_cvd/launch.h"
 #include "host/commands/run_cvd/runner_defs.h"
 #include "host/commands/run_cvd/process_monitor.h"
@@ -277,88 +278,6 @@
   return config.PerInstancePath("cuttlefish_config.json");
 }
 
-template<typename T>
-void AppendVector(std::vector<T>* destination, const std::vector<T>& source) {
-  destination->insert(destination->end(), source.begin(), source.end());
-}
-
-template<typename S, typename T>
-static std::string concat(const S& s, const T& t) {
-  std::ostringstream os;
-  os << s << t;
-  return os.str();
-}
-
-std::vector<std::string> KernelCommandLineFromConfig(const vsoc::CuttlefishConfig& config) {
-  std::vector<std::string> kernel_cmdline;
-
-  AppendVector(&kernel_cmdline, config.boot_image_kernel_cmdline());
-  AppendVector(&kernel_cmdline,
-               vm_manager::VmManager::ConfigureGpuMode(config.vm_manager(), config.gpu_mode()));
-  AppendVector(&kernel_cmdline, vm_manager::VmManager::ConfigureBootDevices(config.vm_manager()));
-
-  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()));
-  }
-  if (config.enable_tombstone_receiver()) {
-    kernel_cmdline.push_back("androidboot.tombstone_transmit=1");
-    kernel_cmdline.push_back(concat(
-        "androidboot.vsock_tombstone_port=",
-        config.tombstone_receiver_port()));
-    // TODO (b/128842613) populate a cid flag to read the host CID during
-    // runtime
-  } else {
-    kernel_cmdline.push_back("androidboot.tombstone_transmit=0");
-  }
-  kernel_cmdline.push_back(concat(
-      "androidboot.cuttlefish_config_server_port=", config.config_server_port()));
-  kernel_cmdline.push_back(concat(
-      "androidboot.setupwizard_mode=", config.setupwizard_mode()));
-  if (!config.use_bootloader()) {
-    std::string slot_suffix;
-    if (config.boot_slot().empty()) {
-      slot_suffix = "_a";
-    } else {
-      slot_suffix = "_" + config.boot_slot();
-    }
-    kernel_cmdline.push_back(concat("androidboot.slot_suffix=", slot_suffix));
-  }
-  kernel_cmdline.push_back(concat("loop.max_part=", config.loop_max_part()));
-  if (config.guest_enforce_security()) {
-    kernel_cmdline.push_back("enforcing=1");
-  } else {
-    kernel_cmdline.push_back("enforcing=0");
-    kernel_cmdline.push_back("androidboot.selinux=permissive");
-  }
-  if (config.guest_audit_security()) {
-    kernel_cmdline.push_back("audit=1");
-  } else {
-    kernel_cmdline.push_back("audit=0");
-  }
-
-  AppendVector(&kernel_cmdline, config.extra_kernel_cmdline());
-
-  return kernel_cmdline;
-}
-std::vector<std::string> KernelCommandLineFromVnc(const VncServerPorts& vnc_ports) {
-  std::vector<std::string> kernel_args;
-  if (vnc_ports.frames_server_vsock_port) {
-    kernel_args.push_back(concat("androidboot.vsock_frames_port=",
-                                 *vnc_ports.frames_server_vsock_port));
-  }
-  if (vnc_ports.touch_server_vsock_port) {
-    kernel_args.push_back(concat("androidboot.vsock_touch_port=",
-                                 *vnc_ports.touch_server_vsock_port));
-  }
-  if (vnc_ports.keyboard_server_vsock_port) {
-    kernel_args.push_back(concat("androidboot.vsock_keyboard_port=",
-                                 *vnc_ports.keyboard_server_vsock_port));
-  }
-  return kernel_args;
-}
-
 }  // namespace
 
 int main(int argc, char** argv) {
@@ -489,7 +408,8 @@
 
   LaunchConfigServer(*config, &process_monitor);
 
-  LaunchTombstoneReceiverIfEnabled(*config, &process_monitor);
+  auto tombstone_server = LaunchTombstoneReceiverIfEnabled(*config, &process_monitor);
+  auto tombstone_kernel_args = KernelCommandLineFromTombstone(tombstone_server);
 
   LaunchUsbServerIfEnabled(*config, &process_monitor);
 
@@ -502,6 +422,8 @@
 
   auto kernel_args = KernelCommandLineFromConfig(*config);
   kernel_args.insert(kernel_args.end(), vnc_kernel_args.begin(), vnc_kernel_args.end());
+  kernel_args.insert(kernel_args.end(), tombstone_kernel_args.begin(),
+                     tombstone_kernel_args.end());
 
   // Start the guest VM
   vm_manager->WithFrontend(vnc_kernel_args.size() > 0);
diff --git a/host/commands/tombstone_receiver/Android.bp b/host/commands/tombstone_receiver/Android.bp
index bd32cae..66a353a 100644
--- a/host/commands/tombstone_receiver/Android.bp
+++ b/host/commands/tombstone_receiver/Android.bp
@@ -28,9 +28,7 @@
         "libcuttlefish_utils",
     ],
     static_libs: [
-        "libcuttlefish_host_config",
         "libgflags",
-        "libjsoncpp",
     ],
     defaults: ["cuttlefish_host_only"],
 }
diff --git a/host/commands/tombstone_receiver/main.cpp b/host/commands/tombstone_receiver/main.cpp
index 7bea670..25da1a3 100644
--- a/host/commands/tombstone_receiver/main.cpp
+++ b/host/commands/tombstone_receiver/main.cpp
@@ -23,7 +23,6 @@
 #include <sstream>
 
 #include "common/libs/fs/shared_fd.h"
-#include "host/libs/config/cuttlefish_config.h"
 
 DEFINE_int32(
     server_fd, -1,
@@ -59,21 +58,13 @@
   ::android::base::InitLogging(argv, android::base::StderrLogger);
   google::ParseCommandLineFlags(&argc, &argv, true);
 
-  auto config = vsoc::CuttlefishConfig::Get();
-  cvd::SharedFD server_fd;
+  cvd::SharedFD server_fd = cvd::SharedFD::Dup(FLAGS_server_fd);
+  close(FLAGS_server_fd);
 
-  if (FLAGS_server_fd < 0) {
-    unsigned int port = config->tombstone_receiver_port();
-    server_fd = cvd::SharedFD::VsockServer(port, SOCK_STREAM);
-  } else {
-    server_fd = cvd::SharedFD::Dup(FLAGS_server_fd);
-    close(FLAGS_server_fd);
-  }
-
-  CHECK(server_fd->IsOpen()) << "Error creating/inheriting tombstone server: "
+  CHECK(server_fd->IsOpen()) << "Error inheriting tombstone server: "
                              << server_fd->StrError();
   LOG(INFO) << "Host is starting server on port "
-            << config->tombstone_receiver_port();
+            << server_fd->VsockServerPort();
 
   // Server loop
   while (true) {
diff --git a/host/libs/config/cuttlefish_config.cpp b/host/libs/config/cuttlefish_config.cpp
index 1ada1a0..c47aacb 100644
--- a/host/libs/config/cuttlefish_config.cpp
+++ b/host/libs/config/cuttlefish_config.cpp
@@ -147,7 +147,6 @@
 const char* kConfigServerBinary = "config_server_binary";
 
 const char* kRunTombstoneReceiver = "enable_tombstone_logger";
-const char* kTombstoneReceiverPort = "tombstone_logger_port";
 const char* kTombstoneReceiverBinary = "tombstone_receiver_binary";
 
 const char* kBootloader = "bootloader";
@@ -746,10 +745,6 @@
   (*dictionary_)[kTombstoneReceiverBinary] = e2e_test_binary;
 }
 
-void CuttlefishConfig::set_tombstone_receiver_port(int port) {
-  (*dictionary_)[kTombstoneReceiverPort] = port;
-}
-
 bool CuttlefishConfig::use_bootloader() const {
   return (*dictionary_)[kUseBootloader].asBool();
 }
@@ -774,10 +769,6 @@
   return (*dictionary_)[kBootSlot].asString();
 }
 
-int CuttlefishConfig::tombstone_receiver_port() const {
-  return (*dictionary_)[kTombstoneReceiverPort].asInt();
-}
-
 std::string CuttlefishConfig::touch_socket_path() const {
   return PerInstanceInternalPath("touch.sock");
 }
diff --git a/host/libs/config/cuttlefish_config.h b/host/libs/config/cuttlefish_config.h
index d6a162a..072c2c8 100644
--- a/host/libs/config/cuttlefish_config.h
+++ b/host/libs/config/cuttlefish_config.h
@@ -294,9 +294,6 @@
   void set_tombstone_receiver_binary(const std::string& binary);
   std::string tombstone_receiver_binary() const;
 
-  void set_tombstone_receiver_port(int port);
-  int tombstone_receiver_port() const;
-
   void set_use_bootloader(bool use_bootloader);
   bool use_bootloader() const;