forked from lix-project/lix
Use RemoteStore
to open connection for proxying daemon
Removes duplicate websocket opening code, and also means we should be able to to ssh-ssh-... daemon relays, not just uds-uds-... ones.
This commit is contained in:
parent
d5af5763cf
commit
a83694c7a1
|
@ -86,7 +86,16 @@ RemoteStore::RemoteStore(const Params & params)
|
||||||
: Store(params)
|
: Store(params)
|
||||||
, connections(make_ref<Pool<Connection>>(
|
, connections(make_ref<Pool<Connection>>(
|
||||||
std::max(1, (int) maxConnections),
|
std::max(1, (int) maxConnections),
|
||||||
[this]() { return openConnectionWrapper(); },
|
[this]() {
|
||||||
|
auto conn = openConnectionWrapper();
|
||||||
|
try {
|
||||||
|
initConnection(*conn);
|
||||||
|
} catch (...) {
|
||||||
|
failed = true;
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
return conn;
|
||||||
|
},
|
||||||
[this](const ref<Connection> & r) {
|
[this](const ref<Connection> & r) {
|
||||||
return
|
return
|
||||||
r->to.good()
|
r->to.good()
|
||||||
|
@ -169,8 +178,6 @@ ref<RemoteStore::Connection> UDSRemoteStore::openConnection()
|
||||||
|
|
||||||
conn->startTime = std::chrono::steady_clock::now();
|
conn->startTime = std::chrono::steady_clock::now();
|
||||||
|
|
||||||
initConnection(*conn);
|
|
||||||
|
|
||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -102,8 +102,6 @@ public:
|
||||||
|
|
||||||
void flushBadConnections();
|
void flushBadConnections();
|
||||||
|
|
||||||
protected:
|
|
||||||
|
|
||||||
struct Connection
|
struct Connection
|
||||||
{
|
{
|
||||||
AutoCloseFD fd;
|
AutoCloseFD fd;
|
||||||
|
@ -119,6 +117,8 @@ protected:
|
||||||
|
|
||||||
ref<Connection> openConnectionWrapper();
|
ref<Connection> openConnectionWrapper();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
virtual ref<Connection> openConnection() = 0;
|
virtual ref<Connection> openConnection() = 0;
|
||||||
|
|
||||||
void initConnection(Connection & conn);
|
void initConnection(Connection & conn);
|
||||||
|
|
|
@ -89,7 +89,6 @@ ref<RemoteStore::Connection> SSHStore::openConnection()
|
||||||
+ (remoteStore.get() == "" ? "" : " --store " + shellEscape(remoteStore.get())));
|
+ (remoteStore.get() == "" ? "" : " --store " + shellEscape(remoteStore.get())));
|
||||||
conn->to = FdSink(conn->sshConn->in.get());
|
conn->to = FdSink(conn->sshConn->in.get());
|
||||||
conn->from = FdSource(conn->sshConn->out.get());
|
conn->from = FdSource(conn->sshConn->out.get());
|
||||||
initConnection(*conn);
|
|
||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -286,46 +286,28 @@ static int _main(int argc, char * * argv)
|
||||||
initPlugins();
|
initPlugins();
|
||||||
|
|
||||||
if (stdio) {
|
if (stdio) {
|
||||||
if (openUncachedStore().dynamic_pointer_cast<UDSRemoteStore>()) {
|
if (auto store = openUncachedStore().dynamic_pointer_cast<RemoteStore>()) {
|
||||||
// FIXME Use the connection the UDSRemoteStore opened
|
auto conn = store->openConnectionWrapper();
|
||||||
|
int from = conn->from.fd;
|
||||||
|
int to = conn->to.fd;
|
||||||
|
|
||||||
// Forward on this connection to the real daemon
|
auto nfds = std::max(from, to) + 1;
|
||||||
auto socketPath = settings.nixDaemonSocketFile;
|
|
||||||
auto s = socket(PF_UNIX, SOCK_STREAM, 0);
|
|
||||||
if (s == -1)
|
|
||||||
throw SysError("creating Unix domain socket");
|
|
||||||
|
|
||||||
auto socketDir = dirOf(socketPath);
|
|
||||||
if (chdir(socketDir.c_str()) == -1)
|
|
||||||
throw SysError("changing to socket directory '%1%'", socketDir);
|
|
||||||
|
|
||||||
auto socketName = std::string(baseNameOf(socketPath));
|
|
||||||
auto addr = sockaddr_un{};
|
|
||||||
addr.sun_family = AF_UNIX;
|
|
||||||
if (socketName.size() + 1 >= sizeof(addr.sun_path))
|
|
||||||
throw Error("socket name %1% is too long", socketName);
|
|
||||||
strcpy(addr.sun_path, socketName.c_str());
|
|
||||||
|
|
||||||
if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) == -1)
|
|
||||||
throw SysError("cannot connect to daemon at %1%", socketPath);
|
|
||||||
|
|
||||||
auto nfds = (s > STDIN_FILENO ? s : STDIN_FILENO) + 1;
|
|
||||||
while (true) {
|
while (true) {
|
||||||
fd_set fds;
|
fd_set fds;
|
||||||
FD_ZERO(&fds);
|
FD_ZERO(&fds);
|
||||||
FD_SET(s, &fds);
|
FD_SET(from, &fds);
|
||||||
FD_SET(STDIN_FILENO, &fds);
|
FD_SET(STDIN_FILENO, &fds);
|
||||||
if (select(nfds, &fds, nullptr, nullptr, nullptr) == -1)
|
if (select(nfds, &fds, nullptr, nullptr, nullptr) == -1)
|
||||||
throw SysError("waiting for data from client or server");
|
throw SysError("waiting for data from client or server");
|
||||||
if (FD_ISSET(s, &fds)) {
|
if (FD_ISSET(from, &fds)) {
|
||||||
auto res = splice(s, nullptr, STDOUT_FILENO, nullptr, SSIZE_MAX, SPLICE_F_MOVE);
|
auto res = splice(from, nullptr, STDOUT_FILENO, nullptr, SSIZE_MAX, SPLICE_F_MOVE);
|
||||||
if (res == -1)
|
if (res == -1)
|
||||||
throw SysError("splicing data from daemon socket to stdout");
|
throw SysError("splicing data from daemon socket to stdout");
|
||||||
else if (res == 0)
|
else if (res == 0)
|
||||||
throw EndOfFile("unexpected EOF from daemon socket");
|
throw EndOfFile("unexpected EOF from daemon socket");
|
||||||
}
|
}
|
||||||
if (FD_ISSET(STDIN_FILENO, &fds)) {
|
if (FD_ISSET(STDIN_FILENO, &fds)) {
|
||||||
auto res = splice(STDIN_FILENO, nullptr, s, nullptr, SSIZE_MAX, SPLICE_F_MOVE);
|
auto res = splice(STDIN_FILENO, nullptr, to, nullptr, SSIZE_MAX, SPLICE_F_MOVE);
|
||||||
if (res == -1)
|
if (res == -1)
|
||||||
throw SysError("splicing data from stdin to daemon socket");
|
throw SysError("splicing data from stdin to daemon socket");
|
||||||
else if (res == 0)
|
else if (res == 0)
|
||||||
|
|
Loading…
Reference in a new issue