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
#pragma once
///@file

#include "lix/libstore/build/request.capnp.h"
#include "lix/libutil/rpc.hh"
#include <boost/format.hpp>
#include <capnp/message.h>
#include <capnp/serialize.h>
#include <cstring>
#include <exception>
#include <memory>
#include <string>
#include <string_view>
#include <unistd.h>
#include <vector>

namespace nix {
bool prepareChildSetup(nix::build::Request::Reader request);
void finishChildSetup(nix::build::Request::Reader request);

[[noreturn]]
void execBuilder(nix::build::Request::Reader request);

// silence the foreign exception lint for this helper
class BaseException : public std::exception
{};

class SysError : public BaseException
{
private:
    std::shared_ptr<std::string> msg;

public:
    explicit SysError(auto fmt, const auto &... args) : SysError(errno, fmt, args...) {}
    SysError(int error, auto fmt, const auto &... args)
    {
        const auto errstr = strerror(error);
        auto format = boost::format(fmt);
        ((format % args), ...);
        msg = std::make_shared<std::string>(format.str() + ": " + errstr);
    }

    const char * what() const noexcept override
    {
        return msg->c_str();
    }
};

struct ExecRequest
{
    std::string builder;
    std::vector<std::string> argsStorage, envsStorage;
    std::vector<char *> args, envs;

    ExecRequest(nix::build::Request::Reader request);
};

void writeFull(int fd, std::string_view data);

extern bool printDebugLogs;

inline void printDebugLog(auto fmt, const auto &... args)
{
    auto format = boost::format(fmt);
    ((format % args), ...);

    capnp::MallocMessageBuilder builder;
    auto log = builder.getRoot<build::SetupResponse>();
    RPC_FILL(log, setLogLine, format.str());
    capnp::writeMessageToFd(STDIN_FILENO, builder);
}

#define debug(msg, ...)                      \
    do {                                     \
        if (::nix::printDebugLogs) {         \
            printDebugLog(msg, __VA_ARGS__); \
        }                                    \
    } while (0)
}