Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
processes.hh
Go to the documentation of this file.
1#pragma once
3
4#include "nix/util/types.hh"
5#include "nix/util/error.hh"
6#include "nix/util/fun.hh"
8#include "nix/util/logging.hh"
10#include "nix/util/os-string.hh"
11
12#include <filesystem>
13
14#include <sys/types.h>
15#include <sys/stat.h>
16#include <dirent.h>
17#include <unistd.h>
18#include <signal.h>
19
20#include <atomic>
21#include <functional>
22#include <map>
23#include <sstream>
24#include <optional>
25#include <thread>
26
27namespace nix {
28
29struct Sink;
30struct Source;
31
32namespace unix {
33#ifndef _WIN32
34constexpr static pid_t INVALID_PID = -1;
35#endif
36}; // namespace unix
37
38class Pid
39{
40#ifndef _WIN32
41 pid_t pid = unix::INVALID_PID;
42 bool separatePG = false;
43 int killSignal = SIGKILL;
44 std::chrono::milliseconds killTimeout;
45 std::thread killThread;
46#else
47 AutoCloseFD pid = INVALID_DESCRIPTOR;
48#endif
49public:
50 Pid();
51 Pid(const Pid &) = delete;
52 Pid(Pid && other) noexcept;
53 Pid & operator=(const Pid &) = delete;
54 Pid & operator=(Pid && other) noexcept;
55#ifndef _WIN32
56 Pid(pid_t pid);
57 void operator=(pid_t pid);
58 operator pid_t() const;
59#else
60 Pid(AutoCloseFD pid);
61 void operator=(AutoCloseFD pid);
62#endif
63 ~Pid();
64
78 explicit operator bool() const noexcept;
79
80 int kill(bool allowInterrupts = true);
81 int wait(bool allowInterrupts = true);
82
83 // TODO: Implement for Windows
84#ifndef _WIN32
85 void setSeparatePG(bool separatePG);
86 void setKillSignal(int signal);
87 void setKillTimeout(std::chrono::milliseconds duration);
88 pid_t release();
89#endif
90
91 friend void swap(Pid & lhs, Pid & rhs) noexcept
92 {
93 using std::swap;
94#ifndef _WIN32
95 swap(lhs.pid, rhs.pid);
96 swap(lhs.separatePG, rhs.separatePG);
97 swap(lhs.killSignal, rhs.killSignal);
98#else
99 swap(lhs.pid, rhs.pid);
100#endif
101 }
102};
103
104#ifndef _WIN32
109void killUser(uid_t uid);
110#endif
111
117{
118 std::string errorPrefix = "";
119 bool dieWithParent = true;
123 int cloneFlags = 0;
124};
125
126#ifndef _WIN32
127pid_t startProcess(fun<void()> processMain, const ProcessOptions & options = ProcessOptions());
128#endif
129
134const std::filesystem::path & shProgram();
135
140std::string runProgram(
141 std::filesystem::path program,
142 bool lookupPath = false,
143 const OsStrings & args = OsStrings(),
144 bool isInteractive = false);
145
150{
151 std::filesystem::path program;
152 bool lookupPath = true;
153 OsStrings args;
154#ifndef _WIN32
155 std::optional<std::string> argv0;
156 std::optional<uid_t> uid;
157 std::optional<uid_t> gid;
158#endif
159 std::optional<std::filesystem::path> chdir;
160 std::optional<OsStringMap> environment;
166 bool dieWithParent = true;
167 bool setSid = false;
173 bool restoreMounts = true;
174};
175
177{
178 SpawnOptions spawnOptions;
179 Sink * standardOut = nullptr;
180 bool mergeStderrToStdout = false;
181 bool isInteractive = false;
182};
183
185{
186 Descriptor from, to;
187
188#ifdef _WIN32
189 /* These are just dummy values to represent distinct file descriptors one might want
190 to redirect. On Windows we'll do equality checks on the "from"/"to" to implement
191 the same dup housekeeping semantics as on POSIX. */
192 static inline const Descriptor stdInput = reinterpret_cast<HANDLE>(static_cast<LONG_PTR>(STD_INPUT_HANDLE));
193 static inline const Descriptor stdOut = reinterpret_cast<HANDLE>(static_cast<LONG_PTR>(STD_OUTPUT_HANDLE));
194 static inline const Descriptor stdError = reinterpret_cast<HANDLE>(static_cast<LONG_PTR>(STD_ERROR_HANDLE));
195#else
196 static constexpr Descriptor stdInput = STDIN_FILENO;
197 static constexpr Descriptor stdOut = STDOUT_FILENO;
198 static constexpr Descriptor stdError = STDERR_FILENO;
199#endif
200};
201
202// Output = error code + "standard out" output stream
203std::pair<int, std::string> runProgram(RunOptions && options);
204
217Pid spawnProgram(const SpawnOptions & options, std::span<const FdRedirection> fdr);
218
219void runProgram2(const RunOptions & options);
220
221class ExecError final : public CloneableError<ExecError, Error>
222{
223 void anchor() override;
224
225public:
226 int status;
227
228 template<typename... Args>
229 ExecError(int status, Args &&... args)
230 : CloneableError(std::forward<Args>(args)...)
231 , status(status)
232 {
233 }
234};
235
240std::string statusToString(int status);
241
242bool statusOk(int status);
243
244} // namespace nix
Some ANSI escape sequences.
Definition args.hh:29
Definition file-descriptor.hh:265
Definition processes.hh:39
Definition fun.hh:19
This file defines two main structs/classes used in nix error handling.
File descriptor operations for almost arbitrary file descriptors.
int Descriptor
Definition file-descriptor.hh:36
std::list< OsString > OsStrings
Definition os-string.hh:51
const std::filesystem::path & shProgram()
Definition processes.cc:11
std::string statusToString(int status)
Definition processes.cc:393
std::string runProgram(std::filesystem::path program, bool lookupPath=false, const OsStrings &args=OsStrings(), bool isInteractive=false)
Definition processes.cc:39
void killUser(uid_t uid)
Definition processes.cc:175
Definition processes.hh:185
Definition processes.hh:117
int cloneFlags
Definition processes.hh:123
Definition processes.hh:177
Definition serialise.hh:18
Definition serialise.hh:95
Definition processes.hh:150
bool dieWithParent
(Linux-only) whether to have the process die when its parent (us) exists.
Definition processes.hh:166
bool restoreMounts
(Linux-only) whether to restore the mount ns in the child. Only set this to false if the child proces...
Definition processes.hh:173