2003-07-20 19:29:38 +00:00
|
|
|
#include <iostream>
|
2003-12-22 16:40:46 +00:00
|
|
|
#include <cstdio>
|
2003-07-20 19:29:38 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "exec.hh"
|
|
|
|
#include "util.hh"
|
|
|
|
#include "globals.hh"
|
|
|
|
|
|
|
|
|
2003-08-19 13:07:38 +00:00
|
|
|
static string pathNullDevice = "/dev/null";
|
|
|
|
|
|
|
|
|
2003-07-20 19:29:38 +00:00
|
|
|
/* Run a program. */
|
2003-08-15 12:32:37 +00:00
|
|
|
void runProgram(const string & program,
|
2003-10-22 11:04:57 +00:00
|
|
|
const Strings & args, const Environment & env,
|
|
|
|
const string & logFileName)
|
2003-07-20 19:29:38 +00:00
|
|
|
{
|
|
|
|
/* Create a log file. */
|
2003-08-29 13:12:30 +00:00
|
|
|
string logCommand =
|
|
|
|
verbosity >= lvlDebug
|
2003-10-22 11:04:57 +00:00
|
|
|
? "tee " + logFileName + " >&2"
|
|
|
|
: "cat > " + logFileName;
|
2003-07-20 19:29:38 +00:00
|
|
|
/* !!! auto-pclose on exit */
|
2003-08-29 13:12:30 +00:00
|
|
|
FILE * logFile = popen(logCommand.c_str(), "w"); /* !!! escaping */
|
2003-07-20 19:29:38 +00:00
|
|
|
if (!logFile)
|
|
|
|
throw SysError(format("creating log file `%1%'") % logFileName);
|
|
|
|
|
|
|
|
/* Create a temporary directory where the build will take
|
|
|
|
place. */
|
2003-10-02 11:55:38 +00:00
|
|
|
string tmpDir = createTempDir();
|
2003-07-20 19:29:38 +00:00
|
|
|
|
|
|
|
AutoDelete delTmpDir(tmpDir);
|
|
|
|
|
|
|
|
/* Fork a child to build the package. */
|
|
|
|
pid_t pid;
|
|
|
|
switch (pid = fork()) {
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
throw SysError("unable to fork");
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
|
|
try { /* child */
|
|
|
|
|
|
|
|
if (chdir(tmpDir.c_str()) == -1)
|
|
|
|
throw SysError(format("changing into to `%1%'") % tmpDir);
|
|
|
|
|
2003-08-15 12:32:37 +00:00
|
|
|
/* Fill in the arguments. */
|
|
|
|
const char * argArr[args.size() + 2];
|
|
|
|
const char * * p = argArr;
|
|
|
|
string progName = baseNameOf(program);
|
|
|
|
*p++ = progName.c_str();
|
|
|
|
for (Strings::const_iterator i = args.begin();
|
|
|
|
i != args.end(); i++)
|
|
|
|
*p++ = i->c_str();
|
|
|
|
*p = 0;
|
|
|
|
|
|
|
|
/* Fill in the environment. */
|
|
|
|
Strings envStrs;
|
|
|
|
const char * envArr[env.size() + 1];
|
|
|
|
p = envArr;
|
|
|
|
for (Environment::const_iterator i = env.begin();
|
|
|
|
i != env.end(); i++)
|
|
|
|
*p++ = envStrs.insert(envStrs.end(),
|
|
|
|
i->first + "=" + i->second)->c_str();
|
|
|
|
*p = 0;
|
2003-07-20 19:29:38 +00:00
|
|
|
|
|
|
|
/* Dup the log handle into stderr. */
|
|
|
|
if (dup2(fileno(logFile), STDERR_FILENO) == -1)
|
|
|
|
throw SysError("cannot pipe standard error into log file");
|
|
|
|
|
|
|
|
/* Dup stderr to stdin. */
|
|
|
|
if (dup2(STDERR_FILENO, STDOUT_FILENO) == -1)
|
|
|
|
throw SysError("cannot dup stderr into stdout");
|
|
|
|
|
2003-08-19 13:07:38 +00:00
|
|
|
/* Reroute stdin to /dev/null. */
|
|
|
|
int fdDevNull = open(pathNullDevice.c_str(), O_RDWR);
|
|
|
|
if (fdDevNull == -1)
|
|
|
|
throw SysError(format("cannot open `%1%'") % pathNullDevice);
|
|
|
|
if (dup2(fdDevNull, STDIN_FILENO) == -1)
|
|
|
|
throw SysError("cannot dup null device into stdin");
|
|
|
|
|
2003-07-20 19:29:38 +00:00
|
|
|
/* Execute the program. This should not return. */
|
2003-08-15 12:32:37 +00:00
|
|
|
execve(program.c_str(), (char * *) argArr, (char * *) envArr);
|
2003-07-20 19:29:38 +00:00
|
|
|
|
|
|
|
throw SysError(format("unable to execute %1%") % program);
|
|
|
|
|
|
|
|
} catch (exception & e) {
|
|
|
|
cerr << format("build error: %1%\n") % e.what();
|
|
|
|
}
|
|
|
|
_exit(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parent */
|
|
|
|
|
|
|
|
/* Close the logging pipe. Note that this should not cause
|
|
|
|
the logger to exit until builder exits (because the latter
|
|
|
|
has an open file handle to the former). */
|
|
|
|
pclose(logFile);
|
|
|
|
|
|
|
|
/* Wait for the child to finish. */
|
|
|
|
int status;
|
|
|
|
if (waitpid(pid, &status, 0) != pid)
|
|
|
|
throw Error("unable to wait for child");
|
|
|
|
|
|
|
|
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
|
2003-08-19 09:04:47 +00:00
|
|
|
if (keepFailed) {
|
2003-11-09 10:35:45 +00:00
|
|
|
printMsg(lvlTalkative,
|
2003-11-01 21:11:52 +00:00
|
|
|
format("program `%1%' failed; keeping build directory `%2%'")
|
|
|
|
% program % tmpDir);
|
2003-08-19 09:04:47 +00:00
|
|
|
delTmpDir.cancel();
|
|
|
|
}
|
2003-11-01 21:11:52 +00:00
|
|
|
if (WIFEXITED(status))
|
|
|
|
throw Error(format("program `%1%' failed with exit code %2%")
|
|
|
|
% program % WEXITSTATUS(status));
|
|
|
|
else if (WIFSIGNALED(status))
|
|
|
|
throw Error(format("program `%1%' failed due to signal %2%")
|
|
|
|
% program % WTERMSIG(status));
|
|
|
|
else
|
|
|
|
throw Error(format("program `%1%' died abnormally") % program);
|
2003-07-20 19:29:38 +00:00
|
|
|
}
|
|
|
|
}
|