Nix 2.93.3
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{
80 {
81 int from, to;
82 };
83
84 Path program;
85 bool searchPath = true;
86 Strings args = {};
87 std::optional<uid_t> uid = {};
88 std::optional<uid_t> gid = {};
89 std::optional<Path> chdir = {};
90 std::optional<std::map<std::string, std::string>> environment = {};
91 bool captureStdout = false;
92 bool isInteractive = false;
93 std::vector<Redirection> redirections;
94#if __linux__
95 std::set<long> caps;
96#endif
97};
98
99struct [[nodiscard("you must call RunningProgram::wait()")]] RunningProgram
100{
101 friend RunningProgram runProgram2(const RunOptions & options);
102
103private:
104 Path program;
105 Pid pid;
106 std::unique_ptr<Source> stdoutSource;
107 AutoCloseFD stdout_;
108
109 RunningProgram(PathView program, Pid pid, AutoCloseFD stdout);
110
111public:
112 RunningProgram() = default;
113 RunningProgram(RunningProgram &&) = default;
114 RunningProgram & operator=(RunningProgram &&) = default;
115 ~RunningProgram();
116
117 explicit operator bool() const { return bool(pid); }
118
119 std::tuple<pid_t, std::unique_ptr<Source>, int> release();
120
121 int kill();
122 [[nodiscard]]
123 int wait();
124 void waitAndCheck();
125
126 std::optional<int> getStdoutFD() const
127 {
128 return stdout_ ? std::optional(stdout_.get()) : std::nullopt;
129 }
130
131 Source * getStdout() const { return stdoutSource.get(); };
132};
133
134std::pair<int, std::string> runProgram(RunOptions && options);
135
136RunningProgram runProgram2(const RunOptions & options);
137
138class ExecError : public Error
139{
140public:
141 int status;
142
143 template<typename... Args>
144 ExecError(int status, const Args & ... args)
145 : Error(args...), status(status)
146 { }
147};
148
153std::string statusToString(int status);
154
155bool statusOk(int status);
156
157}
Definition args.hh:31
Definition file-descriptor.hh:51
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:80
Definition processes.hh:78
Definition processes.hh:100
Definition serialise.hh:18
Definition serialise.hh:66
std::string Path
Definition types.hh:28