1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "lix/libstore/uds-remote-store.hh"
#include "globals.hh"
#include "lix/libutil/async.hh"
#include "lix/libutil/error.hh"
#include "lix/libutil/file-descriptor.hh"
#include "lix/libutil/result.hh"
#include "lix/libutil/strings.hh"
#include "lix/libutil/unix-domain-socket.hh"
#include "lix/libstore/worker-protocol.hh"

#include <cerrno>
#include <ranges>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#include <cstring>


namespace nix {

std::string UDSRemoteStoreConfig::doc()
{
    return
        #include "uds-remote-store.md"
        ;
}

UDSRemoteStore::UDSRemoteStore(UDSRemoteStoreConfig config)
    : Store(config)
    , RemoteStore(config)
    , config_(std::move(config))
{
}


UDSRemoteStore::UDSRemoteStore(
    const std::string scheme,
    std::string socket_path,
    UDSRemoteStoreConfig config)
    : UDSRemoteStore(std::move(config))
{
    path.emplace(socket_path);
}


std::string UDSRemoteStore::getUri()
{
    if (path) {
        return fmt(
            "unix://%s%s", *path, config().protocol.overridden ? fmt("?protocol=%s", config().protocol) : ""
        );
    } else {
        return "daemon";
    }
}

static void connectToFirstAvailableSocket(AutoCloseFD & sockFD, const std::list<Path> & paths)
{
    for (const auto & socket : paths) {
        try {
            nix::connect(sockFD.get(), socket);
            return;
        } catch (SysError & e) {
            if (e.errNo == EACCES || e.errNo == EPERM || e.errNo == ECONNREFUSED || e.errNo == ENOENT
                || e.errNo == ENOTDIR || e.errNo == ENOTSOCK)
            {
                debug("skipping socket %s: %s", socket, strerror(e.errNo));
            } else {
                throw;
            }
        }
    }
    throw Error("could not connect to any lix socket (tried %s)", concatStringsSep(", ", paths));
}

kj::Promise<Result<ref<RemoteStore::Connection>>> UDSRemoteStore::openConnection()
try {
    auto conn = make_ref<Connection>();

    /* Connect to a daemon that does the privileged work for us. */
    conn->fd = createUnixDomainSocket();

    std::list<Path> candidates;

    if (path) {
        if (config().protocol == "any") {
            candidates.emplace_back(*path + LEGACY_SOCKET_COMBINED);
        } else {
            for (const auto & proto : tokenizeString<std::list<std::string>>(config().protocol.get(), " ,")) {
                if (proto == "legacy-combined") {
                    candidates.emplace_back(*path);
                } else {
                    throw Error("can't connect to %s with unknown daemon protocol %s", *path, proto);
                }
            }
        }
    } else {
        candidates = settings.nixDaemonSockets()
            | std::views::transform([](auto & socket) { return socket.path; })
            | std::ranges::to<std::list<Path>>();
    }

    connectToFirstAvailableSocket(conn->fd, candidates);

    conn->startTime = std::chrono::steady_clock::now();

    co_return conn;
} catch (...) {
    co_return result::current_exception();
}


kj::Promise<Result<void>> UDSRemoteStore::addIndirectRoot(const Path & path)
try {
    auto conn(TRY_AWAIT(getConnection()));
    TRY_AWAIT(conn.sendCommand<unsigned>(WorkerProto::Op::AddIndirectRoot, path));
    co_return result::success();
} catch (...) {
    co_return result::current_exception();
}


void registerUDSRemoteStore() {
    StoreImplementations::add<UDSRemoteStore, UDSRemoteStoreConfig>();
}

}