forked from lix-project/lix
libutil: return Pid from startProcess, not pid_t
Change-Id: Icc8a15090c77f54ea7d9220aadedcd4a19922814
This commit is contained in:
parent
3d155fc509
commit
ce6cb14995
|
@ -354,7 +354,7 @@ RunPager::RunPager()
|
||||||
Pipe toPager;
|
Pipe toPager;
|
||||||
toPager.create();
|
toPager.create();
|
||||||
|
|
||||||
pid = Pid{startProcess([&]() {
|
pid = startProcess([&]() {
|
||||||
if (dup2(toPager.readSide.get(), STDIN_FILENO) == -1)
|
if (dup2(toPager.readSide.get(), STDIN_FILENO) == -1)
|
||||||
throw SysError("dupping stdin");
|
throw SysError("dupping stdin");
|
||||||
if (!getenv("LESS"))
|
if (!getenv("LESS"))
|
||||||
|
@ -366,7 +366,7 @@ RunPager::RunPager()
|
||||||
execlp("less", "less", nullptr);
|
execlp("less", "less", nullptr);
|
||||||
execlp("more", "more", nullptr);
|
execlp("more", "more", nullptr);
|
||||||
throw SysError("executing '%1%'", pager);
|
throw SysError("executing '%1%'", pager);
|
||||||
})};
|
});
|
||||||
|
|
||||||
pid.setKillSignal(SIGINT);
|
pid.setKillSignal(SIGINT);
|
||||||
std_out = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 0);
|
std_out = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 0);
|
||||||
|
|
|
@ -35,7 +35,7 @@ HookInstance::HookInstance()
|
||||||
builderOut.create();
|
builderOut.create();
|
||||||
|
|
||||||
/* Fork the hook. */
|
/* Fork the hook. */
|
||||||
pid = Pid{startProcess([&]() {
|
pid = startProcess([&]() {
|
||||||
|
|
||||||
if (dup2(fromHook.writeSide.get(), STDERR_FILENO) == -1)
|
if (dup2(fromHook.writeSide.get(), STDERR_FILENO) == -1)
|
||||||
throw SysError("cannot pipe standard error into log file");
|
throw SysError("cannot pipe standard error into log file");
|
||||||
|
@ -60,7 +60,7 @@ HookInstance::HookInstance()
|
||||||
execv(buildHook.c_str(), stringsToCharPtrs(args).data());
|
execv(buildHook.c_str(), stringsToCharPtrs(args).data());
|
||||||
|
|
||||||
throw SysError("executing '%s'", buildHook);
|
throw SysError("executing '%s'", buildHook);
|
||||||
})};
|
});
|
||||||
|
|
||||||
pid.setSeparatePG(true);
|
pid.setSeparatePG(true);
|
||||||
fromHook.writeSide.reset();
|
fromHook.writeSide.reset();
|
||||||
|
|
|
@ -906,7 +906,7 @@ void LocalDerivationGoal::startBuilder()
|
||||||
Pipe sendPid;
|
Pipe sendPid;
|
||||||
sendPid.create();
|
sendPid.create();
|
||||||
|
|
||||||
Pid helper{startProcess([&]() {
|
Pid helper = startProcess([&]() {
|
||||||
sendPid.readSide.close();
|
sendPid.readSide.close();
|
||||||
|
|
||||||
/* We need to open the slave early, before
|
/* We need to open the slave early, before
|
||||||
|
@ -930,11 +930,11 @@ void LocalDerivationGoal::startBuilder()
|
||||||
if (usingUserNamespace)
|
if (usingUserNamespace)
|
||||||
options.cloneFlags |= CLONE_NEWUSER;
|
options.cloneFlags |= CLONE_NEWUSER;
|
||||||
|
|
||||||
pid_t child = startProcess([&]() { runChild(); }, options);
|
pid_t child = startProcess([&]() { runChild(); }, options).release();
|
||||||
|
|
||||||
writeFull(sendPid.writeSide.get(), fmt("%d\n", child));
|
writeFull(sendPid.writeSide.get(), fmt("%d\n", child));
|
||||||
_exit(0);
|
_exit(0);
|
||||||
})};
|
});
|
||||||
|
|
||||||
sendPid.writeSide.close();
|
sendPid.writeSide.close();
|
||||||
|
|
||||||
|
@ -1010,10 +1010,10 @@ void LocalDerivationGoal::startBuilder()
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
pid = Pid{startProcess([&]() {
|
pid = startProcess([&]() {
|
||||||
openSlave();
|
openSlave();
|
||||||
runChild();
|
runChild();
|
||||||
})};
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/* parent */
|
/* parent */
|
||||||
|
@ -1570,7 +1570,7 @@ void LocalDerivationGoal::addDependency(const StorePath & path)
|
||||||
entering its mount namespace, which is not possible
|
entering its mount namespace, which is not possible
|
||||||
in multithreaded programs. So we do this in a
|
in multithreaded programs. So we do this in a
|
||||||
child process.*/
|
child process.*/
|
||||||
Pid child(startProcess([&]() {
|
Pid child = startProcess([&]() {
|
||||||
|
|
||||||
if (usingUserNamespace && (setns(sandboxUserNamespace.get(), 0) == -1))
|
if (usingUserNamespace && (setns(sandboxUserNamespace.get(), 0) == -1))
|
||||||
throw SysError("entering sandbox user namespace");
|
throw SysError("entering sandbox user namespace");
|
||||||
|
@ -1581,7 +1581,7 @@ void LocalDerivationGoal::addDependency(const StorePath & path)
|
||||||
doBind(source, target);
|
doBind(source, target);
|
||||||
|
|
||||||
_exit(0);
|
_exit(0);
|
||||||
}));
|
});
|
||||||
|
|
||||||
int status = child.wait();
|
int status = child.wait();
|
||||||
if (status != 0)
|
if (status != 0)
|
||||||
|
|
|
@ -70,7 +70,7 @@ std::unique_ptr<SSHMaster::Connection> SSHMaster::startCommand(const std::string
|
||||||
}
|
}
|
||||||
Finally cleanup = [&]() { logger->resume(); };
|
Finally cleanup = [&]() { logger->resume(); };
|
||||||
|
|
||||||
conn->sshPid = Pid{startProcess([&]() {
|
conn->sshPid = startProcess([&]() {
|
||||||
restoreProcessContext();
|
restoreProcessContext();
|
||||||
|
|
||||||
close(in.writeSide.get());
|
close(in.writeSide.get());
|
||||||
|
@ -99,7 +99,7 @@ std::unique_ptr<SSHMaster::Connection> SSHMaster::startCommand(const std::string
|
||||||
|
|
||||||
// could not exec ssh/bash
|
// could not exec ssh/bash
|
||||||
throw SysError("unable to execute '%s'", args.front());
|
throw SysError("unable to execute '%s'", args.front());
|
||||||
}, options)};
|
}, options);
|
||||||
|
|
||||||
|
|
||||||
in.readSide.reset();
|
in.readSide.reset();
|
||||||
|
@ -147,7 +147,7 @@ Path SSHMaster::startMaster()
|
||||||
if (isMasterRunning())
|
if (isMasterRunning())
|
||||||
return state->socketPath;
|
return state->socketPath;
|
||||||
|
|
||||||
state->sshMaster = Pid{startProcess([&]() {
|
state->sshMaster = startProcess([&]() {
|
||||||
restoreProcessContext();
|
restoreProcessContext();
|
||||||
|
|
||||||
close(out.readSide.get());
|
close(out.readSide.get());
|
||||||
|
@ -160,7 +160,7 @@ Path SSHMaster::startMaster()
|
||||||
execvp(args.begin()->c_str(), stringsToCharPtrs(args).data());
|
execvp(args.begin()->c_str(), stringsToCharPtrs(args).data());
|
||||||
|
|
||||||
throw SysError("unable to execute '%s'", args.front());
|
throw SysError("unable to execute '%s'", args.front());
|
||||||
}, options)};
|
}, options);
|
||||||
|
|
||||||
out.writeSide.reset();
|
out.writeSide.reset();
|
||||||
|
|
||||||
|
|
|
@ -94,7 +94,7 @@ bool userNamespacesSupported()
|
||||||
static auto res = [&]() -> bool
|
static auto res = [&]() -> bool
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
Pid pid{startProcess([&]() { _exit(0); }, {.cloneFlags = CLONE_NEWUSER})};
|
Pid pid = startProcess([&]() { _exit(0); }, {.cloneFlags = CLONE_NEWUSER});
|
||||||
|
|
||||||
auto r = pid.wait();
|
auto r = pid.wait();
|
||||||
assert(!r);
|
assert(!r);
|
||||||
|
@ -115,7 +115,7 @@ bool mountAndPidNamespacesSupported()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
|
||||||
Pid pid{startProcess([&]() {
|
Pid pid = startProcess([&]() {
|
||||||
/* Make sure we don't remount the parent's /proc. */
|
/* Make sure we don't remount the parent's /proc. */
|
||||||
if (mount(0, "/", 0, MS_PRIVATE | MS_REC, 0) == -1)
|
if (mount(0, "/", 0, MS_PRIVATE | MS_REC, 0) == -1)
|
||||||
_exit(1);
|
_exit(1);
|
||||||
|
@ -130,7 +130,7 @@ bool mountAndPidNamespacesSupported()
|
||||||
_exit(0);
|
_exit(0);
|
||||||
}, {
|
}, {
|
||||||
.cloneFlags = CLONE_NEWNS | CLONE_NEWPID | (userNamespacesSupported() ? CLONE_NEWUSER : 0)
|
.cloneFlags = CLONE_NEWNS | CLONE_NEWPID | (userNamespacesSupported() ? CLONE_NEWUSER : 0)
|
||||||
})};
|
});
|
||||||
|
|
||||||
if (pid.wait()) {
|
if (pid.wait()) {
|
||||||
debug("PID namespaces do not work on this system: cannot remount /proc");
|
debug("PID namespaces do not work on this system: cannot remount /proc");
|
||||||
|
|
|
@ -183,7 +183,7 @@ static int childEntry(void * arg)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
pid_t startProcess(std::function<void()> fun, const ProcessOptions & options)
|
Pid startProcess(std::function<void()> fun, const ProcessOptions & options)
|
||||||
{
|
{
|
||||||
std::function<void()> wrapper = [&]() {
|
std::function<void()> wrapper = [&]() {
|
||||||
logger = makeSimpleLogger();
|
logger = makeSimpleLogger();
|
||||||
|
@ -227,7 +227,7 @@ pid_t startProcess(std::function<void()> fun, const ProcessOptions & options)
|
||||||
|
|
||||||
if (pid == -1) throw SysError("unable to fork");
|
if (pid == -1) throw SysError("unable to fork");
|
||||||
|
|
||||||
return pid;
|
return Pid{pid};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string runProgram(Path program, bool searchPath, const Strings & args,
|
std::string runProgram(Path program, bool searchPath, const Strings & args,
|
||||||
|
|
|
@ -62,7 +62,8 @@ struct ProcessOptions
|
||||||
int cloneFlags = 0;
|
int cloneFlags = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
pid_t startProcess(std::function<void()> fun, const ProcessOptions & options = ProcessOptions());
|
[[nodiscard]]
|
||||||
|
Pid startProcess(std::function<void()> fun, const ProcessOptions & options = ProcessOptions());
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -369,7 +369,7 @@ static void daemonLoop(std::optional<TrustedFlag> forceTrustClientOpt)
|
||||||
processConnection(openUncachedStore(), from, to, trusted, NotRecursive);
|
processConnection(openUncachedStore(), from, to, trusted, NotRecursive);
|
||||||
|
|
||||||
exit(0);
|
exit(0);
|
||||||
}, options);
|
}, options).release();
|
||||||
|
|
||||||
} catch (Interrupted & e) {
|
} catch (Interrupted & e) {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -22,7 +22,7 @@ RunningProcess RunningProcess::start(std::string executable, Strings args)
|
||||||
procStdout.create();
|
procStdout.create();
|
||||||
|
|
||||||
// This is separate from runProgram2 because we have different IO requirements
|
// This is separate from runProgram2 because we have different IO requirements
|
||||||
pid_t pid = startProcess([&]() {
|
auto pid = startProcess([&]() {
|
||||||
if (dup2(procStdout.writeSide.get(), STDOUT_FILENO) == -1) {
|
if (dup2(procStdout.writeSide.get(), STDOUT_FILENO) == -1) {
|
||||||
throw SysError("dupping stdout");
|
throw SysError("dupping stdout");
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,7 @@ RunningProcess RunningProcess::start(std::string executable, Strings args)
|
||||||
procStdin.readSide.close();
|
procStdin.readSide.close();
|
||||||
|
|
||||||
return RunningProcess{
|
return RunningProcess{
|
||||||
.pid = pid,
|
.pid = std::move(pid),
|
||||||
.procStdin = std::move(procStdin),
|
.procStdin = std::move(procStdin),
|
||||||
.procStdout = std::move(procStdout),
|
.procStdout = std::move(procStdout),
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,13 +7,14 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "file-descriptor.hh"
|
#include "file-descriptor.hh"
|
||||||
|
#include "processes.hh"
|
||||||
#include "tests/terminal-code-eater.hh"
|
#include "tests/terminal-code-eater.hh"
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
struct RunningProcess
|
struct RunningProcess
|
||||||
{
|
{
|
||||||
pid_t pid;
|
Pid pid;
|
||||||
Pipe procStdin;
|
Pipe procStdin;
|
||||||
Pipe procStdout;
|
Pipe procStdout;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue