2020-10-12 17:08:52 +00:00
|
|
|
#include "globals.hh"
|
|
|
|
#include "hook-instance.hh"
|
2009-01-12 16:30:32 +00:00
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
namespace nix {
|
|
|
|
|
2010-08-25 20:44:28 +00:00
|
|
|
HookInstance::HookInstance()
|
|
|
|
{
|
2017-07-30 11:27:57 +00:00
|
|
|
debug("starting build hook '%s'", settings.buildHook);
|
2012-07-27 13:59:18 +00:00
|
|
|
|
2010-08-25 20:44:28 +00:00
|
|
|
/* Create a pipe to get the output of the child. */
|
|
|
|
fromHook.create();
|
2012-07-27 13:59:18 +00:00
|
|
|
|
2010-08-25 20:44:28 +00:00
|
|
|
/* Create the communication pipes. */
|
|
|
|
toHook.create();
|
|
|
|
|
2010-08-30 14:53:03 +00:00
|
|
|
/* Create a pipe to get the output of the builder. */
|
|
|
|
builderOut.create();
|
|
|
|
|
2010-08-25 20:44:28 +00:00
|
|
|
/* Fork the hook. */
|
2014-07-10 14:50:51 +00:00
|
|
|
pid = startProcess([&]() {
|
2012-07-27 13:59:18 +00:00
|
|
|
|
2014-07-10 14:50:51 +00:00
|
|
|
commonChildInit(fromHook);
|
2010-08-25 20:44:28 +00:00
|
|
|
|
2014-08-20 15:00:17 +00:00
|
|
|
if (chdir("/") == -1) throw SysError("changing into /");
|
2010-08-25 20:44:28 +00:00
|
|
|
|
2014-07-10 14:50:51 +00:00
|
|
|
/* Dup the communication pipes. */
|
2016-07-11 19:44:44 +00:00
|
|
|
if (dup2(toHook.readSide.get(), STDIN_FILENO) == -1)
|
2014-07-10 14:50:51 +00:00
|
|
|
throw SysError("dupping to-hook read side");
|
2010-08-25 20:44:28 +00:00
|
|
|
|
2014-07-10 14:50:51 +00:00
|
|
|
/* Use fd 4 for the builder's stdout/stderr. */
|
2016-07-11 19:44:44 +00:00
|
|
|
if (dup2(builderOut.writeSide.get(), 4) == -1)
|
2014-07-10 14:50:51 +00:00
|
|
|
throw SysError("dupping builder's stdout/stderr");
|
2012-07-27 13:59:18 +00:00
|
|
|
|
2018-03-20 14:17:59 +00:00
|
|
|
/* Hack: pass the read side of that fd to allow build-remote
|
|
|
|
to read SSH error messages. */
|
|
|
|
if (dup2(builderOut.readSide.get(), 5) == -1)
|
|
|
|
throw SysError("dupping builder's stdout/stderr");
|
|
|
|
|
2016-01-07 14:10:14 +00:00
|
|
|
Strings args = {
|
2019-12-05 18:11:09 +00:00
|
|
|
std::string(baseNameOf(settings.buildHook.get())),
|
2017-05-02 11:44:10 +00:00
|
|
|
std::to_string(verbosity),
|
2016-01-07 14:10:14 +00:00
|
|
|
};
|
|
|
|
|
2017-05-01 13:46:47 +00:00
|
|
|
execv(settings.buildHook.get().c_str(), stringsToCharPtrs(args).data());
|
2010-08-25 20:44:28 +00:00
|
|
|
|
2017-07-30 11:27:57 +00:00
|
|
|
throw SysError("executing '%s'", settings.buildHook);
|
2014-07-10 14:50:51 +00:00
|
|
|
});
|
2011-06-30 15:19:13 +00:00
|
|
|
|
2010-08-25 20:44:28 +00:00
|
|
|
pid.setSeparatePG(true);
|
2016-07-11 19:44:44 +00:00
|
|
|
fromHook.writeSide = -1;
|
|
|
|
toHook.readSide = -1;
|
2017-10-23 18:43:04 +00:00
|
|
|
|
|
|
|
sink = FdSink(toHook.writeSide.get());
|
2018-03-27 16:41:31 +00:00
|
|
|
std::map<std::string, Config::SettingInfo> settings;
|
|
|
|
globalConfig.getSettings(settings);
|
|
|
|
for (auto & setting : settings)
|
|
|
|
sink << 1 << setting.first << setting.second.value;
|
2017-10-23 18:43:04 +00:00
|
|
|
sink << 0;
|
2010-08-25 20:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HookInstance::~HookInstance()
|
|
|
|
{
|
|
|
|
try {
|
2016-07-11 19:44:44 +00:00
|
|
|
toHook.writeSide = -1;
|
2017-03-16 09:52:28 +00:00
|
|
|
if (pid != -1) pid.kill();
|
2010-08-25 20:44:28 +00:00
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|