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