* Drain the output of the build hook to show error messages. Ugly

hack.
This commit is contained in:
Eelco Dolstra 2004-05-18 14:52:35 +00:00
parent 19479899fb
commit 5e4a2272bf
1 changed files with 26 additions and 4 deletions

View File

@ -635,12 +635,12 @@ void Normaliser::startBuildChild(Goal & goal)
} }
string readLine(int fd) static string readLine(int fd)
{ {
string s; string s;
while (1) { while (1) {
char ch; char ch;
int rd = read(fd, &ch, 1); ssize_t rd = read(fd, &ch, 1);
if (rd == -1) { if (rd == -1) {
if (errno != EINTR) if (errno != EINTR)
throw SysError("reading a line"); throw SysError("reading a line");
@ -654,13 +654,28 @@ string readLine(int fd)
} }
void writeLine(int fd, string s) static void writeLine(int fd, string s)
{ {
s += '\n'; s += '\n';
writeFull(fd, (const unsigned char *) s.c_str(), s.size()); writeFull(fd, (const unsigned char *) s.c_str(), s.size());
} }
/* !!! ugly hack */
static void drain(int fd)
{
unsigned char buffer[1024];
while (1) {
ssize_t rd = read(fd, buffer, sizeof buffer);
if (rd == -1) {
if (errno != EINTR)
throw SysError("draining");
} else if (rd == 0) break;
else writeFull(STDERR_FILENO, buffer, rd);
}
}
Normaliser::HookReply Normaliser::tryBuildHook(Goal & goal) Normaliser::HookReply Normaliser::tryBuildHook(Goal & goal)
{ {
Path buildHook = getEnv("NIX_BUILD_HOOK"); Path buildHook = getEnv("NIX_BUILD_HOOK");
@ -715,12 +730,19 @@ Normaliser::HookReply Normaliser::tryBuildHook(Goal & goal)
whether the hook wishes to perform the build. !!! potential whether the hook wishes to perform the build. !!! potential
for deadlock here: we should also read from the child's logger for deadlock here: we should also read from the child's logger
pipe. */ pipe. */
string reply = readLine(goal.fromHook.readSide); string reply;
try {
reply = readLine(goal.fromHook.readSide);
} catch (Error & e) {
drain(goal.logPipe.readSide);
throw;
}
debug(format("hook reply is `%1%'") % reply); debug(format("hook reply is `%1%'") % reply);
if (reply == "decline" || reply == "postpone") { if (reply == "decline" || reply == "postpone") {
/* Clean up the child. !!! hacky / should verify */ /* Clean up the child. !!! hacky / should verify */
drain(goal.logPipe.readSide);
terminateBuildHook(goal); terminateBuildHook(goal);
return reply == "decline" ? rpDecline : rpPostpone; return reply == "decline" ? rpDecline : rpPostpone;
} }