Move with_control_socket to be a flag on the Subprocess
This puts more into the builder-style configuration and makes the Start
command simpler.
Test: Build and run locally
Change-Id: Ic019d410d59f71279e6ee4c00b53a726356e7181
diff --git a/common/libs/utils/subprocess.cpp b/common/libs/utils/subprocess.cpp
index 9c383d3..aed28de 100644
--- a/common/libs/utils/subprocess.cpp
+++ b/common/libs/utils/subprocess.cpp
@@ -303,26 +303,30 @@
exit_with_parent_ = exit_with_parent;
}
-Subprocess Command::StartHelper(bool with_control_socket, bool in_group) const {
+void Command::SetWithControlSocket(bool with_control_socket) {
+ with_control_socket_ = with_control_socket;
+}
+
+Subprocess Command::StartHelper(bool in_group) const {
auto cmd = ToCharPointers(command_);
if (use_parent_env_) {
return subprocess_impl(cmd.data(), nullptr, redirects_, inherited_fds_,
- with_control_socket, subprocess_stopper_, in_group,
+ with_control_socket_, subprocess_stopper_, in_group,
verbose_, exit_with_parent_);
} else {
auto envp = ToCharPointers(env_);
return subprocess_impl(cmd.data(), envp.data(), redirects_, inherited_fds_,
- with_control_socket, subprocess_stopper_, in_group,
+ with_control_socket_, subprocess_stopper_, in_group,
verbose_, exit_with_parent_);
}
}
-Subprocess Command::Start(bool with_control_socket) const {
- return StartHelper(with_control_socket, false);
+Subprocess Command::Start() const {
+ return StartHelper(false);
}
-Subprocess Command::StartInGroup(bool with_control_socket) const {
- return StartHelper(with_control_socket, true);
+Subprocess Command::StartInGroup() const {
+ return StartHelper(true);
}
// A class that waits for threads to exit in its destructor.
diff --git a/common/libs/utils/subprocess.h b/common/libs/utils/subprocess.h
index df3d9f6..67e5ae2 100644
--- a/common/libs/utils/subprocess.h
+++ b/common/libs/utils/subprocess.h
@@ -121,7 +121,7 @@
SubprocessStopper stopper = KillSubprocess,
bool exit_with_parent = true)
: subprocess_stopper_(stopper), verbose_(true),
- exit_with_parent_(exit_with_parent) {
+ exit_with_parent_(exit_with_parent), with_control_socket_(false) {
command_.push_back(executable);
}
Command(Command&&) = default;
@@ -162,15 +162,15 @@
void SetVerbose(bool verbose);
void SetExitWithParent(bool exit_with_parent);
+ // If with_control_socket is true the returned Subprocess instance will have a
+ // sharedFD that enables communication with the child process.
+ void SetWithControlSocket(bool with_control_socket);
// Starts execution of the command. This method can be called multiple times,
- // effectively staring multiple (possibly concurrent) instances. If
- // with_control_socket is true the returned Subprocess instance will have a
- // sharedFD that enables communication with the child process.
- Subprocess Start(bool with_control_socket = false) const;
- // Same as Start(bool), but the subprocess runs as head of its own process
- // group.
- Subprocess StartInGroup(bool with_control_socket = false) const;
+ // effectively staring multiple (possibly concurrent) instances.
+ Subprocess Start() const;
+ // Same as Start(), but the subprocess runs as head of its own process group.
+ Subprocess StartInGroup() const;
std::string GetShortName() const {
// This is safe because the constructor guarantees the name of the binary to
@@ -179,7 +179,7 @@
}
private:
- Subprocess StartHelper(bool with_control_socket, bool in_group) const;
+ Subprocess StartHelper(bool in_group) const;
std::vector<std::string> command_;
std::map<cvd::SharedFD, int> inherited_fds_{};
@@ -189,6 +189,7 @@
SubprocessStopper subprocess_stopper_;
bool verbose_;
bool exit_with_parent_;
+ bool with_control_socket_;
};
/*
diff --git a/host/commands/assemble_cvd/flags.cc b/host/commands/assemble_cvd/flags.cc
index 2d6007e..da1082d 100644
--- a/host/commands/assemble_cvd/flags.cc
+++ b/host/commands/assemble_cvd/flags.cc
@@ -518,7 +518,7 @@
return false;
}
decomp_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut, output_file);
- auto decomp_proc = decomp_cmd.Start(false);
+ auto decomp_proc = decomp_cmd.Start();
return decomp_proc.Started() && decomp_proc.Wait() == 0;
}
diff --git a/host/commands/run_cvd/process_monitor.cc b/host/commands/run_cvd/process_monitor.cc
index 8243306..f497ddb 100644
--- a/host/commands/run_cvd/process_monitor.cc
+++ b/host/commands/run_cvd/process_monitor.cc
@@ -68,7 +68,8 @@
}
void ProcessMonitor::StartSubprocess(Command cmd, OnSocketReadyCb callback) {
- auto proc = cmd.StartInGroup(true);
+ cmd.SetWithControlSocket(true);
+ auto proc = cmd.StartInGroup();
if (!proc.Started()) {
LOG(ERROR) << "Failed to start process";
return;
@@ -151,7 +152,7 @@
LOG(INFO) << "subprocess " << entry->cmd->GetShortName() << " (" << wait_ret
<< ") has exited for unknown reasons";
}
- entry->proc.reset(new Subprocess(entry->cmd->Start(true)));
+ entry->proc.reset(new Subprocess(entry->cmd->Start()));
return true;
}