Make ReadMonitorSocketLoopForStop a member of ProcessMonitor

The loop only takes "stop" command, and stops the host processes.
We will extend this loop to take other commands, such as resume
and suspend. As the suspend/resume would need to know more about
the set of managed processes, it makes sense that we make the
function a private member function of the ProcessMonitor class

Bug: 287509075
Test: cvd start --daemon && cvd stop
Change-Id: I4035084798bc230aa98f573418bcdeb9741b928d
diff --git a/host/commands/run_cvd/process_monitor.cc b/host/commands/run_cvd/process_monitor.cc
index 18d9fea..eb7b060 100644
--- a/host/commands/run_cvd/process_monitor.cc
+++ b/host/commands/run_cvd/process_monitor.cc
@@ -28,6 +28,7 @@
 #include <algorithm>
 #include <atomic>
 #include <cstdint>
+#include <functional>
 #include <future>
 #include <memory>
 #include <string>
@@ -87,23 +88,6 @@
   return {};
 }
 
-Result<void> ReadMonitorSocketLoopForStop(std::atomic_bool& running,
-                                          SharedFD& monitor_socket) {
-  LOG(DEBUG) << "Waiting for a `stop` message from the parent";
-  while (running.load()) {
-    using process_monitor_impl::ParentToChildMessage;
-    auto message = CF_EXPECT(ParentToChildMessage::Read(monitor_socket));
-    if (message.Stop()) {
-      running.store(false);
-      // Wake up the wait() loop by giving it an exited child process
-      if (fork() == 0) {
-        std::exit(0);
-      }
-    }
-  }
-  return {};
-}
-
 Result<void> MonitorLoop(const std::atomic_bool& running,
                          const bool restart_subprocesses,
                          std::vector<MonitorEntry>& monitored) {
@@ -172,6 +156,23 @@
 
 }  // namespace
 
+Result<void> ProcessMonitor::ReadMonitorSocketLoopForStop(
+    std::atomic_bool& running) {
+  LOG(DEBUG) << "Waiting for a `stop` message from the parent";
+  while (running.load()) {
+    using process_monitor_impl::ParentToChildMessage;
+    auto message = CF_EXPECT(ParentToChildMessage::Read(monitor_socket_));
+    if (message.Stop()) {
+      running.store(false);
+      // Wake up the wait() loop by giving it an exited child process
+      if (fork() == 0) {
+        std::exit(0);
+      }
+    }
+  }
+  return {};
+}
+
 ProcessMonitor::Properties& ProcessMonitor::Properties::RestartSubprocesses(
     bool r) & {
   restart_subprocesses_ = r;
@@ -254,9 +255,13 @@
   StartSubprocesses(properties_.entries_);
 
   std::atomic_bool running(true);
-  auto parent_comms =
-      std::async(std::launch::async, ReadMonitorSocketLoopForStop,
-                 std::ref(running), std::ref(monitor_socket_));
+  auto read_monitor_socket_loop =
+      [this](std::atomic_bool& running) -> Result<void> {
+    CF_EXPECT(this->ReadMonitorSocketLoopForStop(running));
+    return {};
+  };
+  auto parent_comms = std::async(std::launch::async, read_monitor_socket_loop,
+                                 std::ref(running));
 
   MonitorLoop(running, properties_.restart_subprocesses_, properties_.entries_);
   CF_EXPECT(parent_comms.get(), "Should have exited if monitoring stopped");
diff --git a/host/commands/run_cvd/process_monitor.h b/host/commands/run_cvd/process_monitor.h
index 7edb77b..ca95160 100644
--- a/host/commands/run_cvd/process_monitor.h
+++ b/host/commands/run_cvd/process_monitor.h
@@ -15,6 +15,7 @@
  */
 #pragma once
 
+#include <atomic>
 #include <memory>
 #include <mutex>
 #include <thread>
@@ -76,6 +77,7 @@
 
  private:
   Result<void> MonitorRoutine();
+  Result<void> ReadMonitorSocketLoopForStop(std::atomic_bool&);
 
   Properties properties_;
   pid_t monitor_;