#pragma once ///@file #include "types.hh" #include "error.hh" #include "logging.hh" #include "ansicolor.hh" #include #include #include #include #include #include #include #include #include #include #include namespace nix { struct Sink; struct Source; class Pid { pid_t pid = -1; bool separatePG = false; int killSignal = SIGKILL; public: Pid(); Pid(pid_t pid); ~Pid(); void operator =(pid_t pid); operator pid_t(); int kill(); int wait(); void setSeparatePG(bool separatePG); void setKillSignal(int signal); pid_t release(); }; /** * Kill all processes running under the specified uid by sending them * a SIGKILL. */ void killUser(uid_t uid); /** * Fork a process that runs the given function, and return the child * pid to the caller. */ struct ProcessOptions { std::string errorPrefix = ""; bool dieWithParent = true; bool runExitHandlers = false; bool allowVfork = false; /** * use clone() with the specified flags (Linux only) */ int cloneFlags = 0; }; pid_t startProcess(std::function fun, const ProcessOptions & options = ProcessOptions()); /** * Run a program and return its stdout in a string (i.e., like the * shell backtick operator). */ std::string runProgram(Path program, bool searchPath = false, const Strings & args = Strings(), const std::optional & input = {}, bool isInteractive = false); struct RunOptions { Path program; bool searchPath = true; Strings args; std::optional uid; std::optional gid; std::optional chdir; std::optional> environment; std::optional input; Source * standardIn = nullptr; Sink * standardOut = nullptr; bool mergeStderrToStdout = false; bool isInteractive = false; }; std::pair runProgram(RunOptions && options); void runProgram2(const RunOptions & options); class ExecError : public Error { public: int status; template 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); }