Merge pull request #1340 from NixOS/start-using-nix-ssh

Replace `Child` with `SSHMaster::Connection`
This commit is contained in:
John Ericson 2024-01-23 01:17:26 -05:00 committed by GitHub
commit f5c0efb11e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -14,19 +14,13 @@
#include "util.hh" #include "util.hh"
#include "serve-protocol.hh" #include "serve-protocol.hh"
#include "serve-protocol-impl.hh" #include "serve-protocol-impl.hh"
#include "ssh.hh"
#include "finally.hh" #include "finally.hh"
#include "url.hh" #include "url.hh"
using namespace nix; using namespace nix;
struct Child
{
Pid pid;
AutoCloseFD to, from;
};
static void append(Strings & dst, const Strings & src) static void append(Strings & dst, const Strings & src)
{ {
dst.insert(dst.end(), src.begin(), src.end()); dst.insert(dst.end(), src.begin(), src.end());
@ -54,7 +48,7 @@ static Strings extraStoreArgs(std::string & machine)
return result; return result;
} }
static void openConnection(Machine::ptr machine, Path tmpDir, int stderrFD, Child & child) static void openConnection(Machine::ptr machine, Path tmpDir, int stderrFD, SSHMaster::Connection & child)
{ {
std::string pgmName; std::string pgmName;
Pipe to, from; Pipe to, from;
@ -84,7 +78,7 @@ static void openConnection(Machine::ptr machine, Path tmpDir, int stderrFD, Chil
append(argv, extraArgs); append(argv, extraArgs);
} }
child.pid = startProcess([&]() { child.sshPid = startProcess([&]() {
restoreProcessContext(); restoreProcessContext();
if (dup2(to.readSide.get(), STDIN_FILENO) == -1) if (dup2(to.readSide.get(), STDIN_FILENO) == -1)
@ -104,8 +98,8 @@ static void openConnection(Machine::ptr machine, Path tmpDir, int stderrFD, Chil
to.readSide = -1; to.readSide = -1;
from.writeSide = -1; from.writeSide = -1;
child.to = to.writeSide.release(); child.in = to.writeSide.release();
child.from = from.readSide.release(); child.out = from.readSide.release();
} }
@ -488,13 +482,13 @@ void State::buildRemote(ref<Store> destStore,
updateStep(ssConnecting); updateStep(ssConnecting);
// FIXME: rewrite to use Store. // FIXME: rewrite to use Store.
Child child; SSHMaster::Connection child;
build_remote::openConnection(machine, tmpDir, logFD.get(), child); build_remote::openConnection(machine, tmpDir, logFD.get(), child);
{ {
auto activeStepState(activeStep->state_.lock()); auto activeStepState(activeStep->state_.lock());
if (activeStepState->cancelled) throw Error("step cancelled"); if (activeStepState->cancelled) throw Error("step cancelled");
activeStepState->pid = child.pid; activeStepState->pid = child.sshPid;
} }
Finally clearPid([&]() { Finally clearPid([&]() {
@ -510,8 +504,8 @@ void State::buildRemote(ref<Store> destStore,
}); });
Machine::Connection conn { Machine::Connection conn {
.from = child.from.get(), .from = child.out.get(),
.to = child.to.get(), .to = child.in.get(),
.machine = machine, .machine = machine,
}; };
@ -523,7 +517,7 @@ void State::buildRemote(ref<Store> destStore,
try { try {
build_remote::handshake(conn, buildOptions.repeats); build_remote::handshake(conn, buildOptions.repeats);
} catch (EndOfFile & e) { } catch (EndOfFile & e) {
child.pid.wait(); child.sshPid.wait();
std::string s = chomp(readFile(result.logFile)); std::string s = chomp(readFile(result.logFile));
throw Error("cannot connect to %1%: %2%", machine->sshName, s); throw Error("cannot connect to %1%: %2%", machine->sshName, s);
} }
@ -617,8 +611,8 @@ void State::buildRemote(ref<Store> destStore,
} }
/* Shut down the connection. */ /* Shut down the connection. */
child.to = -1; child.in = -1;
child.pid.wait(); child.sshPid.wait();
} catch (Error & e) { } catch (Error & e) {
/* Disable this machine until a certain period of time has /* Disable this machine until a certain period of time has