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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#pragma once
///@file

#include "lix/libutil/async-io.hh"
#include "lix/libutil/result.hh"
#include "lix/libutil/types.hh"
#include "lix/libutil/error.hh"
#include "lix/libutil/file-descriptor.hh"

#include <kj/async.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>

#include <functional>
#include <map>
#include <optional>

namespace nix {

struct Sink;
struct Source;

class Pid
{
    pid_t pid = -1;
public:
    Pid();
    explicit Pid(pid_t pid): pid(pid) {}
    Pid(Pid && other);
    Pid & operator=(Pid && other);
    ~Pid() noexcept(false);
    explicit operator bool() const { return pid != -1; }
    int kill();
    int wait();

    pid_t release();
    pid_t get() const { return pid; }
};

class ProcessGroup
{
    Pid leader;

public:
    ProcessGroup() = default;
    explicit ProcessGroup(Pid leader) : leader(std::move(leader)) {}
    ProcessGroup(ProcessGroup &&) = default;
    ProcessGroup & operator=(ProcessGroup &&) = default;
    ~ProcessGroup() noexcept(false);

    explicit operator bool() const
    {
        return bool(leader);
    }

    int wait()
    {
        return leader.wait();
    }
    int kill();
};

/**
 * Kill all processes running under the specified uid by sending them
 * a SIGKILL.
 */
void killUser(uid_t uid);

/**
 * Run a program and return its stdout in a string (i.e., like the
 * shell backtick operator).
 */
kj::Promise<Result<std::string>> runProgram(
    Path program,
    bool searchPath = false,
    const Strings args = Strings(),
    bool isInteractive = false
);

struct RunOptions
{
    struct Redirection
    {
        int dup, from;
    };

    Path program;
    bool searchPath = true;
    std::optional<std::string> argv0;
    Strings args = {};
    std::optional<std::map<std::string, std::string>> environment = {};
    bool captureStdout = false;
    std::vector<Redirection> redirections;
    bool keepContext = false;
};

struct [[nodiscard("you must call RunningProgram::wait()")]] RunningProgram
{
    friend RunningProgram runProgram2(const RunOptions & options);

protected:
    Path program;
    Pid pid;
    std::unique_ptr<AsyncFdIoStream> childStdout;

    RunningProgram(PathView program, Pid pid, AutoCloseFD childStdout);

public:
    RunningProgram() = default;
    RunningProgram(RunningProgram &&) = default;
    RunningProgram & operator=(RunningProgram &&) = default;
    ~RunningProgram();

    explicit operator bool() const { return bool(pid); }

    std::tuple<Pid, std::unique_ptr<AsyncFdIoStream>> release();

    int kill();
    [[nodiscard]]
    int wait();
    void waitAndCheck();

    std::optional<int> getStdoutFD() const
    {
        return childStdout ? std::optional(childStdout->getFD()) : std::nullopt;
    }

    AsyncFdIoStream * getStdout() const
    {
        return childStdout.get();
    };
};

kj::Promise<Result<std::pair<int, std::string>>> runProgram(RunOptions options);

RunningProgram runProgram2(const RunOptions & options);

class [[nodiscard("you must call RunningHelper::wait()")]] RunningHelper : RunningProgram
{
    friend RunningHelper runHelper(const char * name, RunOptions options);

    const char * name;
    AutoCloseFD errPipe;

    RunningHelper(const char * name, RunningProgram && proc, AutoCloseFD && errPipe)
        : RunningProgram(std::move(proc))
        , name(name)
        , errPipe(std::move(errPipe))
    {
    }

    void check();

public:
    RunningHelper() = default;
    RunningHelper(RunningHelper &&) = default;
    RunningHelper & operator=(RunningHelper &&) = default;

    using RunningProgram::operator bool;
    using RunningProgram::getStdout;
    using RunningProgram::getStdoutFD;
    using RunningProgram::kill;
    using RunningProgram::wait;

    /**
     * Kill the entire process group the helper runs in. This is usually **unsafe**
     * unless the helper process has made itself a process group or session leader!
     */
    int killProcessGroup();

    void waitAndCheck();
};

RunningHelper runHelper(const char * name, RunOptions options);

class ExecError : public Error
{
public:
    int status;

    template<typename... Args>
    ExecError(int status, const Args & ... args)
        : Error(args...), status(status)
    { }
};

/**
 * Convert the exit status of a child as returned by wait() into an
 * error string.
 */
std::string statusToString(int status);

bool statusOk(int status);

}