diff options
author | Tom Hubrecht <github@mail.hubrecht.ovh> | 2024-05-28 13:14:13 +0200 |
---|---|---|
committer | Tom Hubrecht <github@mail.hubrecht.ovh> | 2024-05-29 11:01:34 +0200 |
commit | 9a52e4688ca265155817817f373938428f023966 (patch) | |
tree | b90975deb9def6b8cb7c3ffe2510b7318df37d93 /src/libutil/processes.hh | |
parent | 8cd9aa24a8d48caf22225ce32dff3c5aaa111687 (diff) |
util.{hh,cc}: Split out processes.{hh,cc}
Change-Id: I39280dc40ca3f7f9007bc6c898ffcf760e2238b7
Diffstat (limited to 'src/libutil/processes.hh')
-rw-r--r-- | src/libutil/processes.hh | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/libutil/processes.hh b/src/libutil/processes.hh new file mode 100644 index 000000000..91a4edfd2 --- /dev/null +++ b/src/libutil/processes.hh @@ -0,0 +1,115 @@ +#pragma once +///@file + +#include "types.hh" +#include "error.hh" + +#include <sys/types.h> +#include <sys/stat.h> +#include <dirent.h> +#include <unistd.h> +#include <signal.h> + +#include <boost/lexical_cast.hpp> + +#include <functional> +#include <map> +#include <optional> + +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() noexcept(false); + 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; + /** + * use clone() with the specified flags (Linux only) + */ + int cloneFlags = 0; +}; + +pid_t startProcess(std::function<void()> 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<std::string> & input = {}, bool isInteractive = false); + +struct RunOptions +{ + Path program; + bool searchPath = true; + Strings args; + std::optional<uid_t> uid; + std::optional<uid_t> gid; + std::optional<Path> chdir; + std::optional<std::map<std::string, std::string>> environment; + std::optional<std::string> input; + Source * standardIn = nullptr; + Sink * standardOut = nullptr; + bool mergeStderrToStdout = false; + bool isInteractive = false; +}; + +std::pair<int, std::string> runProgram(RunOptions && options); + +void runProgram2(const 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); + +} |