}
bool RealCommandRunner::StartCommand(Edge* edge) {
- string err;
string command = edge->EvaluateCommand();
printf(" %s\n", command.c_str());
Subprocess* subproc = new Subprocess;
subproc_to_edge_.insert(make_pair(subproc, edge));
- if (!subproc->Start(command, &err))
+ if (!subproc->Start(command))
return false;
subprocs_.Add(subproc);
Subprocess::~Subprocess() {
}
-bool Subprocess::Start(const string& command, string* err) {
+bool Subprocess::Start(const string& command) {
int stdout_pipe[2];
- if (pipe(stdout_pipe) < 0) {
- *err = strerror(errno);
- return false;
- }
+ if (pipe(stdout_pipe) < 0)
+ Fatal("pipe: %s", strerror(errno));
stdout_.fd_ = stdout_pipe[0];
int stderr_pipe[2];
- if (pipe(stderr_pipe) < 0) {
- *err = strerror(errno);
- return false;
- }
+ if (pipe(stderr_pipe) < 0)
+ Fatal("pipe: %s", strerror(errno));
stderr_.fd_ = stderr_pipe[0];
pid_ = fork();
- if (pid_ < 0) {
- *err = strerror(errno);
- return false;
- } else if (pid_ == 0) {
+ if (pid_ < 0)
+ Fatal("fork: %s", strerror(errno));
+
+ if (pid_ == 0) {
if (close(0) < 0 ||
dup2(stdout_pipe[1], 1) < 0 ||
dup2(stderr_pipe[1], 2) < 0 ||
struct Subprocess {
Subprocess();
~Subprocess();
- bool Start(const string& command, string* err);
+ bool Start(const string& command);
void OnFDReady(int fd);
bool Finish(string* err);
TEST(Subprocess, Ls) {
Subprocess ls;
- string err;
- EXPECT_TRUE(ls.Start("ls /", &err));
- ASSERT_EQ("", err);
+ EXPECT_TRUE(ls.Start("ls /"));
// Pretend we discovered that stdout was ready for writing.
ls.OnFDReady(ls.stdout_.fd_);
+ string err;
EXPECT_TRUE(ls.Finish(&err));
ASSERT_EQ("", err);
EXPECT_NE("", ls.stdout_.buf_);
TEST(Subprocess, BadCommand) {
Subprocess subproc;
- string err;
- EXPECT_TRUE(subproc.Start("ninja_no_such_command", &err));
- ASSERT_EQ("", err);
+ EXPECT_TRUE(subproc.Start("ninja_no_such_command"));
// Pretend we discovered that stderr was ready for writing.
subproc.OnFDReady(subproc.stderr_.fd_);
+ string err;
EXPECT_FALSE(subproc.Finish(&err));
EXPECT_NE("", err);
EXPECT_EQ("", subproc.stdout_.buf_);
TEST(SubprocessSet, Single) {
SubprocessSet subprocs;
Subprocess* ls = new Subprocess;
- string err;
- EXPECT_TRUE(ls->Start("ls /", &err));
- ASSERT_EQ("", err);
+ EXPECT_TRUE(ls->Start("ls /"));
subprocs.Add(ls);
+ string err;
while (!ls->done()) {
subprocs.DoWork(&err);
ASSERT_EQ("", err);
"pwd",
};
- string err;
for (int i = 0; i < 3; ++i) {
processes[i] = new Subprocess;
- EXPECT_TRUE(processes[i]->Start(kCommands[i], &err));
- ASSERT_EQ("", err);
+ EXPECT_TRUE(processes[i]->Start(kCommands[i]));
subprocs.Add(processes[i]);
}
ASSERT_EQ("", processes[i]->stderr_.buf_);
}
+ string err;
while (!processes[0]->done() || !processes[1]->done() ||
!processes[2]->done()) {
ASSERT_GT(subprocs.running_.size(), 0);