The stdio stream identifiers (stdin, stdout, stderr) are allowed to be macros.
In musl libc they are, for example doing `#define stdout (stdout)`, breaking
compilation with an error when one of the clashing variables is attempted to be
initialized the "wrong" way:
../lix/libutil/processes.cc:272:7: error: expected class member or base class name
272 | , stdout(stdout ? std::make_unique<AsyncFdIoStream>(std::move(stdout)) : nullptr)
| ^
/nix/store/ziw42d7rvgnf3vkbfc8kry07kipwf1xm-musl-static-x86_64-unknown-linux-musl-1.2.5-dev/include/stdio.h:67:16: note: expanded from macro 'stdout'
67 | #define stdout (stdout)
| ^
Other places only cause warnings on musl:
../lix/libutil/processes.cc:254:17: warning: parentheses were disambiguated as redundant parentheses around declaration of variable named 'stdout' [-Wvexing-parse]
254 | std::string stdout;
| ^~~~~~
/nix/store/ziw42d7rvgnf3vkbfc8kry07kipwf1xm-musl-static-x86_64-unknown-linux-musl-1.2.5-dev/include/stdio.h:67:16: note: expanded from macro 'stdout'
67 | #define stdout (stdout)
| ^~~~~~~~
../lix/libutil/processes.cc:254:17: note: add a variable name to declare a 'std::string' (aka 'basic_string<char>') initialized with 'stdout'
254 | std::string stdout;
| ^
| varname
/nix/store/ziw42d7rvgnf3vkbfc8kry07kipwf1xm-musl-static-x86_64-unknown-linux-musl-1.2.5-dev/include/stdio.h:67:16: note: expanded from macro 'stdout'
67 | #define stdout (stdout)
| ^
../lix/libutil/processes.cc:254:5: note: add enclosing parentheses to perform a function-style cast
254 | std::string stdout;
| ^
| ( )
../lix/libutil/processes.cc:254:17: note: remove parentheses to silence this warning
254 | std::string stdout;
| ^
/nix/store/ziw42d7rvgnf3vkbfc8kry07kipwf1xm-musl-static-x86_64-unknown-linux-musl-1.2.5-dev/include/stdio.h:67:16: note: expanded from macro 'stdout'
67 | #define stdout (stdout)
| ^
However they are still wrong, since the macro could be more complicated. Fix
them as well.
Change-Id: I6a6a6964a50ef7dec8f05f0bd8fc8f13f3036d51
166 lines
3.5 KiB
C++
166 lines
3.5 KiB
C++
#pragma once
|
|
///@file
|
|
|
|
#include "lix/libutil/async-io.hh"
|
|
#include "lix/libutil/result.hh"
|
|
#include "lix/libutil/types.hh"
|
|
#include "lix/libutil/error.hh"
|
|
#include "lix/libutil/file-descriptor.hh"
|
|
|
|
#include <kj/async.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <dirent.h>
|
|
#include <unistd.h>
|
|
#include <signal.h>
|
|
|
|
#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();
|
|
explicit Pid(pid_t pid): pid(pid) {}
|
|
Pid(Pid && other);
|
|
Pid & operator=(Pid && other);
|
|
~Pid() noexcept(false);
|
|
explicit operator bool() const { return pid != -1; }
|
|
int kill();
|
|
int wait();
|
|
|
|
void setSeparatePG(bool separatePG);
|
|
void setKillSignal(int signal);
|
|
pid_t release();
|
|
pid_t get() const { return pid; }
|
|
};
|
|
|
|
/**
|
|
* 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
|
|
{
|
|
bool dieWithParent = true;
|
|
/**
|
|
* use clone() with the specified flags (Linux only)
|
|
*/
|
|
int cloneFlags = 0;
|
|
};
|
|
|
|
[[nodiscard]]
|
|
Pid 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).
|
|
*/
|
|
kj::Promise<Result<std::string>> runProgram(
|
|
Path program,
|
|
bool searchPath = false,
|
|
const Strings args = Strings(),
|
|
bool isInteractive = false
|
|
);
|
|
|
|
struct RunOptions
|
|
{
|
|
struct Redirection
|
|
{
|
|
int dup, from;
|
|
};
|
|
|
|
Path program;
|
|
bool searchPath = true;
|
|
std::optional<std::string> argv0;
|
|
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 = {};
|
|
bool captureStdout = false;
|
|
bool isInteractive = false;
|
|
std::vector<Redirection> redirections;
|
|
#if __linux__
|
|
std::set<long> caps;
|
|
#endif
|
|
};
|
|
|
|
struct [[nodiscard("you must call RunningProgram::wait()")]] RunningProgram
|
|
{
|
|
friend RunningProgram runProgram2(const RunOptions & options);
|
|
|
|
private:
|
|
Path program;
|
|
Pid pid;
|
|
std::unique_ptr<AsyncFdIoStream> childStdout;
|
|
|
|
RunningProgram(PathView program, Pid pid, AutoCloseFD childStdout);
|
|
|
|
public:
|
|
RunningProgram() = default;
|
|
RunningProgram(RunningProgram &&) = default;
|
|
RunningProgram & operator=(RunningProgram &&) = default;
|
|
~RunningProgram();
|
|
|
|
explicit operator bool() const { return bool(pid); }
|
|
|
|
std::tuple<pid_t, std::unique_ptr<AsyncFdIoStream>> release();
|
|
|
|
int kill();
|
|
[[nodiscard]]
|
|
int wait();
|
|
void waitAndCheck();
|
|
|
|
std::optional<int> getStdoutFD() const
|
|
{
|
|
return childStdout ? std::optional(childStdout->getFD()) : std::nullopt;
|
|
}
|
|
|
|
AsyncFdIoStream * getStdout() const
|
|
{
|
|
return childStdout.get();
|
|
};
|
|
};
|
|
|
|
kj::Promise<Result<std::pair<int, std::string>>> runProgram(RunOptions options);
|
|
|
|
RunningProgram 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);
|
|
|
|
}
|