Nix  2.93.0-dev
Lix: A modern, delicious implementation of the Nix package manager; unstable internal interfaces
Loading...
Searching...
No Matches
processes.hh
Go to the documentation of this file.
1#pragma once
3
7
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <dirent.h>
11#include <unistd.h>
12#include <signal.h>
13
14#include <functional>
15#include <map>
16#include <optional>
17
18namespace nix {
19
20struct Sink;
21struct Source;
22
23class Pid
24{
25 pid_t pid = -1;
26 bool separatePG = false;
27 int killSignal = SIGKILL;
28public:
29 Pid();
30 explicit Pid(pid_t pid): pid(pid) {}
31 Pid(Pid && other);
32 Pid & operator=(Pid && other);
33 ~Pid() noexcept(false);
34 explicit operator bool() const { return pid != -1; }
35 int kill();
36 int wait();
37
38 void setSeparatePG(bool separatePG);
39 void setKillSignal(int signal);
40 pid_t release();
41 pid_t get() const { return pid; }
42};
43
48void killUser(uid_t uid);
49
50
56{
57 std::string errorPrefix = "";
58 bool dieWithParent = true;
59 bool runExitHandlers = false;
63 int cloneFlags = 0;
64};
65
66[[nodiscard]]
67Pid startProcess(std::function<void()> fun, const ProcessOptions & options = ProcessOptions());
68
69
74std::string runProgram(Path program, bool searchPath = false,
75 const Strings & args = Strings(), bool isInteractive = false);
76
78{
79 Path program;
80 bool searchPath = true;
81 Strings args = {};
82 std::optional<uid_t> uid = {};
83 std::optional<uid_t> gid = {};
84 std::optional<Path> chdir = {};
85 std::optional<std::map<std::string, std::string>> environment = {};
86 bool captureStdout = false;
87 bool mergeStderrToStdout = false;
88 bool isInteractive = false;
89};
90
91struct [[nodiscard("you must call RunningProgram::wait()")]] RunningProgram
92{
93 friend RunningProgram runProgram2(const RunOptions & options);
94
95private:
96 Path program;
97 Pid pid;
98 std::unique_ptr<Source> stdoutSource;
99 AutoCloseFD stdout_;
100
101 RunningProgram(PathView program, Pid pid, AutoCloseFD stdout);
102
103public:
104 RunningProgram() = default;
106
107 void wait();
108
109 Source * getStdout() const { return stdoutSource.get(); };
110};
111
112std::pair<int, std::string> runProgram(RunOptions && options);
113
114RunningProgram runProgram2(const RunOptions & options);
115
116class ExecError : public Error
117{
118public:
119 int status;
120
121 template<typename... Args>
122 ExecError(int status, const Args & ... args)
123 : Error(args...), status(status)
124 { }
125};
126
131std::string statusToString(int status);
132
133bool statusOk(int status);
134
135}
Definition args.hh:31
Definition file-descriptor.hh:42
Definition processes.hh:117
Definition processes.hh:24
This file defines two main structs/classes used in nix error handling.
Definition processes.hh:56
int cloneFlags
Definition processes.hh:63
Definition processes.hh:78
Definition processes.hh:92
Definition serialise.hh:66
std::string Path
Definition types.hh:28