ShellManager: output stderr in the debug buffer

This commit is contained in:
Maxime Coste 2012-09-06 14:28:07 +02:00
parent ddd5236b08
commit 96beaf50dd

View File

@ -1,5 +1,7 @@
#include "shell_manager.hh" #include "shell_manager.hh"
#include "debug.hh"
#include <cstring> #include <cstring>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
@ -23,17 +25,20 @@ String ShellManager::pipe(const String& input,
const String& cmdline, const Context& context, const String& cmdline, const Context& context,
const EnvVarMap& env_vars) const EnvVarMap& env_vars)
{ {
int write_pipe[2]; int write_pipe[2]; // child stdin
int read_pipe[2]; int read_pipe[2]; // child stdout
int error_pipe[2]; // child stderr
::pipe(write_pipe); ::pipe(write_pipe);
::pipe(read_pipe); ::pipe(read_pipe);
::pipe(error_pipe);
String output; String output;
if (pid_t pid = fork()) if (pid_t pid = fork())
{ {
close(write_pipe[0]); close(write_pipe[0]);
close(read_pipe[1]); close(read_pipe[1]);
close(error_pipe[1]);
memoryview<char> data = input.data(); memoryview<char> data = input.data();
write(write_pipe[1], data.pointer(), data.size()); write(write_pipe[1], data.pointer(), data.size());
@ -47,14 +52,30 @@ String ShellManager::pipe(const String& input,
output += String(buffer, buffer+size); output += String(buffer, buffer+size);
} }
close(read_pipe[0]); close(read_pipe[0]);
String errorout;
while (size_t size = read(error_pipe[0], buffer, 1024))
{
if (size == -1)
break;
errorout += String(buffer, buffer+size);
}
close(error_pipe[0]);
if (not errorout.empty())
{
write_debug("\nshell stderr: <<<\n" + errorout + ">>>\n");
}
waitpid(pid, NULL, 0); waitpid(pid, NULL, 0);
} }
else try else try
{ {
close(write_pipe[1]); close(write_pipe[1]);
close(read_pipe[0]); close(read_pipe[0]);
close(error_pipe[0]);
dup2(read_pipe[1], 1); close(read_pipe[1]); dup2(read_pipe[1], 1); close(read_pipe[1]);
dup2(error_pipe[1], 2); close(error_pipe[1]);
dup2(write_pipe[0], 0); close(write_pipe[0]); dup2(write_pipe[0], 0); close(write_pipe[0]);
boost::regex_iterator<String::iterator> it(cmdline.begin(), cmdline.end(), m_regex); boost::regex_iterator<String::iterator> it(cmdline.begin(), cmdline.end(), m_regex);