2020-10-11 19:01:53 +03:00
|
|
|
#include "uds-remote-store.hh"
|
2023-10-25 07:43:36 +03:00
|
|
|
#include "unix-domain-socket.hh"
|
2023-03-23 16:06:45 +02:00
|
|
|
#include "worker-protocol.hh"
|
2020-10-11 20:18:26 +03:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2024-04-18 23:49:52 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include <winsock2.h>
|
|
|
|
# include <afunix.h>
|
|
|
|
#else
|
|
|
|
# include <sys/socket.h>
|
|
|
|
# include <sys/un.h>
|
|
|
|
#endif
|
2020-10-11 20:18:26 +03:00
|
|
|
|
2020-10-11 19:01:53 +03:00
|
|
|
namespace nix {
|
2020-10-11 20:18:26 +03:00
|
|
|
|
2024-07-18 06:32:27 +03:00
|
|
|
UDSRemoteStoreConfig::UDSRemoteStoreConfig(
|
|
|
|
std::string_view scheme,
|
|
|
|
std::string_view authority,
|
|
|
|
const Params & params)
|
|
|
|
: StoreConfig(params)
|
|
|
|
, LocalFSStoreConfig(params)
|
|
|
|
, RemoteStoreConfig(params)
|
|
|
|
, path{authority.empty() ? settings.nixDaemonSocketFile : authority}
|
|
|
|
{
|
|
|
|
if (scheme != UDSRemoteStoreConfig::scheme) {
|
|
|
|
throw UsageError("Scheme must be 'unix'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-12 12:10:55 +03:00
|
|
|
std::string UDSRemoteStoreConfig::doc()
|
|
|
|
{
|
|
|
|
return
|
|
|
|
#include "uds-remote-store.md"
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-18 06:32:27 +03:00
|
|
|
// A bit gross that we now pass empty string but this is knowing that
|
|
|
|
// empty string will later default to the same nixDaemonSocketFile. Why
|
|
|
|
// don't we just wire it all through? I believe there are cases where it
|
|
|
|
// will live reload so we want to continue to account for that.
|
2020-10-11 20:18:26 +03:00
|
|
|
UDSRemoteStore::UDSRemoteStore(const Params & params)
|
2024-07-18 06:32:27 +03:00
|
|
|
: UDSRemoteStore(scheme, "", params)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
UDSRemoteStore::UDSRemoteStore(std::string_view scheme, std::string_view authority, const Params & params)
|
2020-10-11 20:18:26 +03:00
|
|
|
: StoreConfig(params)
|
2020-12-20 17:33:12 +02:00
|
|
|
, LocalFSStoreConfig(params)
|
|
|
|
, RemoteStoreConfig(params)
|
2024-07-18 06:32:27 +03:00
|
|
|
, UDSRemoteStoreConfig(scheme, authority, params)
|
2020-10-11 20:18:26 +03:00
|
|
|
, Store(params)
|
|
|
|
, LocalFSStore(params)
|
|
|
|
, RemoteStore(params)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string UDSRemoteStore::getUri()
|
|
|
|
{
|
2024-07-18 06:32:27 +03:00
|
|
|
return path == settings.nixDaemonSocketFile
|
|
|
|
? // FIXME: Not clear why we return daemon here and not default
|
|
|
|
// to settings.nixDaemonSocketFile
|
|
|
|
//
|
|
|
|
// unix:// with no path also works. Change what we return?
|
|
|
|
"daemon"
|
|
|
|
: std::string(scheme) + "://" + path;
|
2020-10-11 20:18:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-23 19:01:04 +03:00
|
|
|
void UDSRemoteStore::Connection::closeWrite()
|
|
|
|
{
|
2024-04-18 23:49:52 +03:00
|
|
|
shutdown(toSocket(fd.get()), SHUT_WR);
|
2021-09-23 19:01:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-11 20:18:26 +03:00
|
|
|
ref<RemoteStore::Connection> UDSRemoteStore::openConnection()
|
|
|
|
{
|
|
|
|
auto conn = make_ref<Connection>();
|
|
|
|
|
|
|
|
/* Connect to a daemon that does the privileged work for us. */
|
2021-08-16 21:03:32 +03:00
|
|
|
conn->fd = createUnixDomainSocket();
|
2020-10-11 20:18:26 +03:00
|
|
|
|
2024-07-18 06:32:27 +03:00
|
|
|
nix::connect(toSocket(conn->fd.get()), path);
|
2020-10-11 20:18:26 +03:00
|
|
|
|
|
|
|
conn->from.fd = conn->fd.get();
|
|
|
|
conn->to.fd = conn->fd.get();
|
|
|
|
|
|
|
|
conn->startTime = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-23 16:06:45 +02:00
|
|
|
void UDSRemoteStore::addIndirectRoot(const Path & path)
|
|
|
|
{
|
|
|
|
auto conn(getConnection());
|
|
|
|
conn->to << WorkerProto::Op::AddIndirectRoot << path;
|
|
|
|
conn.processStderr();
|
|
|
|
readInt(conn->from);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-11 20:18:26 +03:00
|
|
|
static RegisterStoreImplementation<UDSRemoteStore, UDSRemoteStoreConfig> regUDSRemoteStore;
|
|
|
|
|
|
|
|
}
|