123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #include <iostream> // Temporary.
- #include <stdexcept>
-
- #include "child.hpp"
-
- namespace CodeDweller {
-
- Child::Child(std::vector<std::string> args, size_t bufSize) :
- readStreambuf(bufSize),
- writeStreambuf(bufSize),
- reader(&readStreambuf),
- writer(&writeStreambuf) {
-
- init();
-
- if (args.size() == 0) {
-
- } else if (args.size() == 1) {
- cmdline = args[0];
- return;
- }
-
-
- for (size_t i = 0; i < args.size() - 1; i++) {
- cmdline += args[i] + " ";
- }
- cmdline += args.back();
- }
-
- Child::Child(std::string childpath, size_t bufSize) :
- readStreambuf(bufSize),
- writeStreambuf(bufSize),
- reader(&readStreambuf),
- writer(&writeStreambuf),
- cmdline(childpath) {
- init();
- }
-
- Child::~Child() {
-
- }
-
- void
- Child::init() {
- childStarted = false;
- exitCodeObtainedFlag = false;
- exitCode = 0;
- }
-
- void
- Child::run() {
-
- if (childStarted) {
- throw std::logic_error("Child process was active when "
- "run() was called");
- }
-
-
- SECURITY_ATTRIBUTES securityAttributes;
-
- securityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
- securityAttributes.bInheritHandle = true;
- securityAttributes.lpSecurityDescriptor = NULL;
-
-
- HANDLE childStdOutAtChild;
- HANDLE childStdOutAtParent;
- HANDLE childStdInAtChild;
- HANDLE childStdInAtParent;
- int bufferSize = 0;
-
- if (!CreatePipe(&childStdOutAtParent,
- &childStdOutAtChild,
- &securityAttributes,
- bufferSize)) {
- throw std::runtime_error("Error from CreatePipe for stdout: " +
- getErrorText());
- }
-
-
- int inheritFlag = 0;
- if (!SetHandleInformation(childStdOutAtParent,
- HANDLE_FLAG_INHERIT,
- inheritFlag) ) {
- throw std::runtime_error("Error from GetHandleInformation for stdout: " +
- getErrorText());
- }
-
-
- if (! CreatePipe(&childStdInAtChild,
- &childStdInAtParent,
- &securityAttributes,
- bufferSize)) {
- throw std::runtime_error("Error from CreatePipe for stdin: " +
- getErrorText());
- }
-
-
- if (!SetHandleInformation(childStdInAtParent,
- HANDLE_FLAG_INHERIT,
- inheritFlag)) {
- throw std::runtime_error("Error from GetHandleInformation for stdin: " +
- getErrorText());
- }
-
-
- PROCESS_INFORMATION processInfo;
-
- std::fill((char *) &processInfo,
- ((char *) &processInfo) + sizeof(PROCESS_INFORMATION),
- 0);
-
-
-
- STARTUPINFO startInfo;
-
- std::fill((char *) &startInfo,
- ((char *) &startInfo) + sizeof(STARTUPINFO),
- 0);
- startInfo.cb = sizeof(STARTUPINFO);
- startInfo.hStdError = childStdOutAtChild;
- startInfo.hStdOutput = childStdOutAtChild;
- startInfo.hStdInput = childStdInAtChild;
- startInfo.dwFlags |= STARTF_USESTDHANDLES;
-
-
- bool status;
-
- status = CreateProcess(NULL,
- (char *) cmdline.c_str(),
- NULL,
- NULL,
- true,
- 0,
- NULL,
- NULL,
- &startInfo,
- &processInfo);
-
-
- if (!status ) {
- throw std::runtime_error("Error from CreateProcess with "
- "command line \"" + cmdline + "\": " +
- getErrorText());
- }
-
-
-
- readStreambuf.setInputHandle(childStdOutAtParent);
- writeStreambuf.setOutputHandle(childStdInAtParent);
-
-
- childProcess = processInfo.hProcess;
- childThread = processInfo.hThread;
-
- childStarted = true;
-
-
- if (!CloseHandle(childStdOutAtChild)) {
- throw std::runtime_error("Error closing the child process stdout handle: " +
- getErrorText());
- }
-
- if (!CloseHandle(childStdInAtChild)) {
- throw std::runtime_error("Error closing the child process stdin handle: " +
- getErrorText());
- }
-
- }
-
- void
- Child::terminate() {
-
- if (isDone()) {
-
- return;
-
- }
-
- if (!TerminateProcess(childProcess, terminateExitCode)) {
- throw std::runtime_error("Error terminating the child process: " +
- getErrorText());
- }
-
- }
-
- bool
- Child::isDone() {
-
- if (exitCodeObtainedFlag) {
-
- return true;
-
- }
-
- if (!childStarted) {
- throw std::logic_error("Child process was not started "
- "when isDone() was called");
- }
-
- int result;
-
- if (!GetExitCodeProcess(childProcess, (LPDWORD) &result)) {
- throw std::runtime_error("Error checking status of child process: " +
- getErrorText());
- }
-
- if (STILL_ACTIVE == result) {
-
- return false;
-
- }
-
-
- exitCode = result;
- exitCodeObtainedFlag = true;
-
- return true;
-
- }
-
- int32_t
- Child::result() {
-
- if (exitCodeObtainedFlag) {
-
- return exitCode;
-
- }
-
- if (!childStarted) {
- throw std::logic_error("Child process was not started "
- "when result() was called");
- }
-
- int result;
-
- if (!GetExitCodeProcess(childProcess, (LPDWORD) &result)) {
- throw std::runtime_error("Error getting child process exit code: " +
- getErrorText());
- }
-
-
- if (STILL_ACTIVE == result) {
- throw std::logic_error("Child process was active when "
- "result() was called");
- }
-
-
- exitCode = result;
- exitCodeObtainedFlag = true;
-
- return result;
- }
-
- std::string
- Child::getErrorText() {
-
- LPVOID winMsgBuf;
- DWORD lastError = GetLastError();
-
- FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- lastError,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (char *) &winMsgBuf,
- 0, NULL );
-
- std::string errMsg((char *) winMsgBuf);
-
- LocalFree(winMsgBuf);
- return errMsg;
- }
-
- Child::ReadStreambuf::ReadStreambuf(std::size_t bufferSize) :
- inputHandle(0),
- buffer(bufferSize + 1) {
-
- char *end = &(buffer.front()) + buffer.size();
-
-
- setg(end, end, end);
-
- }
-
- void
- Child::ReadStreambuf::setInputHandle(HANDLE inHandle) {
-
- inputHandle = inHandle;
-
- }
-
- std::streambuf::int_type
- Child::ReadStreambuf::underflow() {
-
-
- if (gptr() < egptr()) {
-
-
- return traits_type::to_int_type(*gptr());
-
- }
-
-
- char *base = &(buffer.front());
- char *start = base;
-
-
- if (eback() == base) {
-
-
-
- }
-
-
- DWORD nBytesRead;
-
- if (!ReadFile(inputHandle,
- start,
- buffer.size() - (start - base),
- &nBytesRead,
- NULL)) {
- return traits_type::eof();
- }
-
-
- if (0 == nBytesRead) {
- return traits_type::eof();
- }
-
-
- setg(base, start, start + nBytesRead);
-
- return traits_type::to_int_type(*gptr());
-
- }
-
- Child::WriteStreambuf::WriteStreambuf(std::size_t bufferSize) :
- outputHandle(0),
- buffer(bufferSize + 1) {
-
- char *base = &(buffer.front());
-
-
- setp(base, base + buffer.size() - 1);
-
- }
-
- void
- Child::WriteStreambuf::setOutputHandle(HANDLE outHandle) {
-
- outputHandle = outHandle;
-
- }
-
- void
- Child::WriteStreambuf::flushBuffer() {
-
-
- std::ptrdiff_t nBytes = pptr() - pbase();
- DWORD nBytesWritten;
-
- if (!WriteFile(outputHandle,
- pbase(),
- nBytes,
- &nBytesWritten,
- NULL)) {
- throw std::runtime_error("Error writing to child process: " +
- getErrorText());
- }
- if (nBytes != nBytesWritten) {
- throw std::runtime_error("Not all data was written to to child process: " +
- getErrorText());
- }
- pbump(-nBytes);
-
- return;
-
- }
-
- std::streambuf::int_type
- Child::WriteStreambuf::overflow(int_type ch) {
-
-
- if (traits_type::eof() != ch) {
-
-
- *(pptr()) = ch;
- pbump(1);
-
- }
-
-
- flushBuffer();
-
-
- return traits_type::not_eof('a');
-
- }
-
- int
- Child::WriteStreambuf::sync() {
-
- flushBuffer();
- return 1;
-
- }
-
- }
|