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