|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace CodeDweller {
|
|
|
namespace CodeDweller {
|
|
|
|
|
|
|
|
|
ChildStream::ChildStream(std::vector<std::string> args, size_t bufSize) :
|
|
|
|
|
|
|
|
|
ChildStream::ChildStream(std::vector<std::string> const &args,
|
|
|
|
|
|
size_t bufSize) :
|
|
|
childStreambuf(bufSize),
|
|
|
childStreambuf(bufSize),
|
|
|
std::iostream(&childStreambuf),
|
|
|
std::iostream(&childStreambuf),
|
|
|
cmdArgs(args) {
|
|
|
cmdArgs(args) {
|
|
|
|
|
|
|
|
|
init();
|
|
|
init();
|
|
|
|
|
|
run();
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
ChildStream::ChildStream(std::string childpath, size_t bufSize) :
|
|
|
|
|
|
|
|
|
ChildStream::ChildStream(std::string const &childpath, size_t bufSize) :
|
|
|
childStreambuf(bufSize),
|
|
|
childStreambuf(bufSize),
|
|
|
std::iostream(&childStreambuf),
|
|
|
|
|
|
cmdline(childpath) {
|
|
|
|
|
|
|
|
|
std::iostream(&childStreambuf) {
|
|
|
|
|
|
|
|
|
cmdArgs.push_back(childpath);
|
|
|
cmdArgs.push_back(childpath);
|
|
|
init();
|
|
|
init();
|
|
|
|
|
|
run();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
ChildStream::~ChildStream() {
|
|
|
|
|
|
// Close handles.
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
ChildStream::ChildStream(size_t bufSize) :
|
|
|
|
|
|
childStreambuf(bufSize),
|
|
|
|
|
|
std::iostream(&childStreambuf) {
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
ChildStream::init() {
|
|
|
|
|
|
|
|
|
init();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
if (cmdArgs.empty()) {
|
|
|
|
|
|
throw std::invalid_argument("A child executable must be specified.");
|
|
|
|
|
|
|
|
|
ChildStream::~ChildStream() {
|
|
|
|
|
|
if (isRunning()) {
|
|
|
|
|
|
close();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ChildStream::init() {
|
|
|
|
|
|
|
|
|
childStarted = false;
|
|
|
childStarted = false;
|
|
|
childExited = false;
|
|
|
childExited = false;
|
|
|
|
|
|
|
|
|
exitCode = 0;
|
|
|
exitCode = 0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
size_t
|
|
|
|
|
|
ChildStream::numBytesAvailable() const {
|
|
|
|
|
|
|
|
|
void ChildStream::open(std::vector<std::string> const &args) {
|
|
|
|
|
|
cmdArgs = args;
|
|
|
|
|
|
init();
|
|
|
|
|
|
run();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ChildStream::open(std::string const &childpath) {
|
|
|
|
|
|
cmdArgs.clear();
|
|
|
|
|
|
cmdArgs.push_back(childpath);
|
|
|
|
|
|
init();
|
|
|
|
|
|
run();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t ChildStream::numBytesAvailable() const {
|
|
|
|
|
|
|
|
|
return childStreambuf.numBytesAvailable();
|
|
|
return childStreambuf.numBytesAvailable();
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
ChildStream::run() {
|
|
|
|
|
|
|
|
|
bool ChildStream::isRunning() const {
|
|
|
|
|
|
return childStarted && !childExited;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ChildStream::run() {
|
|
|
|
|
|
|
|
|
if (childStarted) {
|
|
|
if (childStarted) {
|
|
|
throw std::logic_error("Child process was active when "
|
|
|
throw std::logic_error("Child process was active when "
|
|
|
"run() was called");
|
|
|
"run() was called");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (cmdArgs.empty()) {
|
|
|
|
|
|
throw std::invalid_argument("A child executable must be specified.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
#ifdef _WIN32
|
|
|
// Set the bInheritHandle flag so pipe handles are inherited.
|
|
|
// Set the bInheritHandle flag so pipe handles are inherited.
|
|
|
SECURITY_ATTRIBUTES securityAttributes;
|
|
|
SECURITY_ATTRIBUTES securityAttributes;
|
|
|
|
|
|
|
|
|
getErrorText());
|
|
|
getErrorText());
|
|
|
}
|
|
|
}
|
|
|
if (pipe(childStdOutPipe) != 0) {
|
|
|
if (pipe(childStdOutPipe) != 0) {
|
|
|
close(childStdInPipe[0]);
|
|
|
|
|
|
close(childStdInPipe[1]);
|
|
|
|
|
|
|
|
|
::close(childStdInPipe[0]);
|
|
|
|
|
|
::close(childStdInPipe[1]);
|
|
|
throw std::runtime_error("Error creating pipe for stdout: " +
|
|
|
throw std::runtime_error("Error creating pipe for stdout: " +
|
|
|
getErrorText());
|
|
|
getErrorText());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
childPid = fork();
|
|
|
childPid = fork();
|
|
|
if (-1 == childPid) {
|
|
|
if (-1 == childPid) {
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
close(childStdInPipe[i]);
|
|
|
|
|
|
close(childStdOutPipe[i]);
|
|
|
|
|
|
|
|
|
::close(childStdInPipe[i]);
|
|
|
|
|
|
::close(childStdOutPipe[i]);
|
|
|
}
|
|
|
}
|
|
|
throw std::runtime_error("Error creating child process: " +
|
|
|
throw std::runtime_error("Error creating child process: " +
|
|
|
getErrorText());
|
|
|
getErrorText());
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
// Close pipes.
|
|
|
// Close pipes.
|
|
|
if ( (close(childStdInPipe[0]) != 0) ||
|
|
|
|
|
|
(close(childStdInPipe[1]) != 0) ||
|
|
|
|
|
|
(close(childStdOutPipe[0]) != 0) ||
|
|
|
|
|
|
(close(childStdOutPipe[1]) != 0) ) {
|
|
|
|
|
|
|
|
|
if ( (::close(childStdInPipe[0]) != 0) ||
|
|
|
|
|
|
(::close(childStdInPipe[1]) != 0) ||
|
|
|
|
|
|
(::close(childStdOutPipe[0]) != 0) ||
|
|
|
|
|
|
(::close(childStdOutPipe[1]) != 0) ) {
|
|
|
std::string errMsg;
|
|
|
std::string errMsg;
|
|
|
|
|
|
|
|
|
// Send message to parent.
|
|
|
// Send message to parent.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
// std::cout << "Child pid: " << childPid << std::endl; // DEBUG.
|
|
|
|
|
|
|
|
|
|
|
|
// Provide the stream buffers with the file descriptors for
|
|
|
// Provide the stream buffers with the file descriptors for
|
|
|
// communicating with the child process.
|
|
|
// communicating with the child process.
|
|
|
childStreambuf.setInputFileDescriptor(childStdOutPipe[0]);
|
|
|
childStreambuf.setInputFileDescriptor(childStdOutPipe[0]);
|
|
|
childStreambuf.setOutputFileDescriptor(childStdInPipe[1]);
|
|
|
childStreambuf.setOutputFileDescriptor(childStdInPipe[1]);
|
|
|
|
|
|
|
|
|
// Close the child's end of the pipes.
|
|
|
// Close the child's end of the pipes.
|
|
|
if ( (close(childStdInPipe[0]) != 0) ||
|
|
|
|
|
|
(close(childStdOutPipe[1]) != 0) ) {
|
|
|
|
|
|
|
|
|
if ( (::close(childStdInPipe[0]) != 0) ||
|
|
|
|
|
|
(::close(childStdOutPipe[1]) != 0) ) {
|
|
|
std::string errMsg;
|
|
|
std::string errMsg;
|
|
|
|
|
|
|
|
|
throw std::runtime_error("Error closing child's end of pipes in "
|
|
|
throw std::runtime_error("Error closing child's end of pipes in "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
ChildStream::terminate() {
|
|
|
|
|
|
|
|
|
void ChildStream::close() {
|
|
|
|
|
|
|
|
|
if (isDone()) {
|
|
|
if (isDone()) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
|
ChildStream::isDone() {
|
|
|
|
|
|
|
|
|
bool ChildStream::isDone() {
|
|
|
|
|
|
|
|
|
if (childExited) {
|
|
|
if (childExited) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
|
|
ChildStream::result() {
|
|
|
|
|
|
|
|
|
int32_t ChildStream::result() {
|
|
|
|
|
|
|
|
|
if (exitCodeObtainedFlag) {
|
|
|
if (exitCodeObtainedFlag) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
std::string
|
|
|
|
|
|
ChildStream::getErrorText() {
|
|
|
|
|
|
|
|
|
std::string ChildStream::getErrorText() {
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
#ifdef _WIN32
|
|
|
LPVOID winMsgBuf;
|
|
|
LPVOID winMsgBuf;
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
#ifdef _WIN32
|
|
|
void
|
|
|
|
|
|
ChildStream::ChildStreambuf::setInputHandle(HANDLE inHandle) {
|
|
|
|
|
|
|
|
|
void ChildStream::ChildStreambuf::setInputHandle(HANDLE inHandle) {
|
|
|
|
|
|
|
|
|
inputHandle = inHandle;
|
|
|
inputHandle = inHandle;
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
#else
|
|
|
#else
|
|
|
void
|
|
|
|
|
|
ChildStream::ChildStreambuf::setInputFileDescriptor(int inFd) {
|
|
|
|
|
|
|
|
|
void ChildStream::ChildStreambuf::setInputFileDescriptor(int inFd) {
|
|
|
|
|
|
|
|
|
inputFileDescriptor = inFd;
|
|
|
inputFileDescriptor = inFd;
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
#endif
|
|
|
#endif
|
|
|
|
|
|
|
|
|
size_t
|
|
|
|
|
|
ChildStream::ChildStreambuf::numBytesAvailable() const {
|
|
|
|
|
|
|
|
|
size_t ChildStream::ChildStreambuf::numBytesAvailable() const {
|
|
|
|
|
|
|
|
|
size_t nBytesAvailable = egptr() - gptr();
|
|
|
size_t nBytesAvailable = egptr() - gptr();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
std::streambuf::int_type
|
|
|
|
|
|
ChildStream::ChildStreambuf::underflow() {
|
|
|
|
|
|
|
|
|
std::streambuf::int_type ChildStream::ChildStreambuf::underflow() {
|
|
|
|
|
|
|
|
|
// Check for empty buffer.
|
|
|
// Check for empty buffer.
|
|
|
if (gptr() < egptr()) {
|
|
|
if (gptr() < egptr()) {
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
#ifdef _WIN32
|
|
|
void
|
|
|
|
|
|
ChildStream::ChildStreambuf::setOutputHandle(HANDLE outHandle) {
|
|
|
|
|
|
|
|
|
void ChildStream::ChildStreambuf::setOutputHandle(HANDLE outHandle) {
|
|
|
|
|
|
|
|
|
outputHandle = outHandle;
|
|
|
outputHandle = outHandle;
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
#else
|
|
|
#else
|
|
|
void
|
|
|
|
|
|
ChildStream::ChildStreambuf::setOutputFileDescriptor(int outFd) {
|
|
|
|
|
|
|
|
|
void ChildStream::ChildStreambuf::setOutputFileDescriptor(int outFd) {
|
|
|
|
|
|
|
|
|
outputFileDescriptor = outFd;
|
|
|
outputFileDescriptor = outFd;
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
#endif
|
|
|
#endif
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
ChildStream::ChildStreambuf::flushBuffer() {
|
|
|
|
|
|
|
|
|
void ChildStream::ChildStreambuf::flushBuffer() {
|
|
|
|
|
|
|
|
|
// Write.
|
|
|
// Write.
|
|
|
std::ptrdiff_t nBytes = pptr() - pbase();
|
|
|
std::ptrdiff_t nBytes = pptr() - pbase();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
std::streambuf::int_type
|
|
|
|
|
|
ChildStream::ChildStreambuf::overflow(int_type ch) {
|
|
|
|
|
|
|
|
|
std::streambuf::int_type ChildStream::ChildStreambuf::overflow(int_type ch) {
|
|
|
|
|
|
|
|
|
// Check whether we're writing EOF.
|
|
|
// Check whether we're writing EOF.
|
|
|
if (traits_type::eof() != ch) {
|
|
|
if (traits_type::eof() != ch) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
|
ChildStream::ChildStreambuf::sync() {
|
|
|
|
|
|
|
|
|
int ChildStream::ChildStreambuf::sync() {
|
|
|
|
|
|
|
|
|
flushBuffer(); // Throws exception on failure.
|
|
|
flushBuffer(); // Throws exception on failure.
|
|
|
|
|
|
|