aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/System/Unix/Program.inc59
-rw-r--r--lib/System/Unix/Unix.h31
-rw-r--r--lib/System/Win32/Program.inc74
-rw-r--r--lib/System/Win32/Win32.h16
4 files changed, 126 insertions, 54 deletions
diff --git a/lib/System/Unix/Program.inc b/lib/System/Unix/Program.inc
index c3d5d5c..97aa804 100644
--- a/lib/System/Unix/Program.inc
+++ b/lib/System/Unix/Program.inc
@@ -81,18 +81,24 @@ Program::FindProgramByName(const std::string& progName) {
return Path();
}
-static void RedirectFD(const std::string &File, int FD) {
- if (File.empty()) return; // Noop
+static bool RedirectFD(const std::string &File, int FD, std::string* ErrMsg) {
+ if (File.empty()) return false; // Noop
// Open the file
int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
if (InFD == -1) {
- ThrowErrno("Cannot open file '" + File + "' for "
+ MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
+ (FD == 0 ? "input" : "output") + "!\n");
+ return true;
}
- dup2(InFD, FD); // Install it as the requested FD
+ // Install it as the requested FD
+ if (-1 == dup2(InFD, FD)) {
+ MakeErrMsg(ErrMsg, "Cannot dup2");
+ return true;
+ }
close(InFD); // Close the original FD
+ return false;
}
static bool Timeout = false;
@@ -105,10 +111,14 @@ Program::ExecuteAndWait(const Path& path,
const char** args,
const char** envp,
const Path** redirects,
- unsigned secondsToWait
-) {
- if (!path.canExecute())
- return -9999;
+ unsigned secondsToWait,
+ std::string* ErrMsg)
+{
+ if (!path.canExecute()) {
+ if (ErrMsg)
+ *ErrMsg = path.toString() + " is not executable";
+ return -1;
+ }
#ifdef HAVE_SYS_WAIT_H
// Create a child process.
@@ -116,9 +126,8 @@ Program::ExecuteAndWait(const Path& path,
switch (child) {
// An error occured: Return to the caller.
case -1:
- ThrowErrno(std::string("Couldn't execute program '") + path.toString() +
- "'");
- break;
+ MakeErrMsg(ErrMsg, "Couldn't fork");
+ return -1;
// Child process: Execute the program.
case 0: {
@@ -126,22 +135,23 @@ Program::ExecuteAndWait(const Path& path,
if (redirects) {
if (redirects[0])
if (redirects[0]->isEmpty())
- RedirectFD("/dev/null",0);
+ if (RedirectFD("/dev/null",0,ErrMsg)) { return -1; }
else
- RedirectFD(redirects[0]->toString(), 0);
+ if (RedirectFD(redirects[0]->toString(), 0,ErrMsg)) { return -1; }
if (redirects[1])
if (redirects[1]->isEmpty())
- RedirectFD("/dev/null",1);
+ if (RedirectFD("/dev/null",1,ErrMsg)) { return -1; }
else
- RedirectFD(redirects[1]->toString(), 1);
+ if (RedirectFD(redirects[1]->toString(),1,ErrMsg)) { return -1; }
if (redirects[1] && redirects[2] &&
*(redirects[1]) != *(redirects[2])) {
if (redirects[2]->isEmpty())
- RedirectFD("/dev/null",2);
+ if (RedirectFD("/dev/null",2,ErrMsg)) { return -1; }
else
- RedirectFD(redirects[2]->toString(), 2);
- } else {
- dup2(1, 2);
+ if (RedirectFD(redirects[2]->toString(), 2,ErrMsg)) { return -1; }
+ } else if (-1 == dup2(1,2)) {
+ MakeErrMsg(ErrMsg, "Can't redirect");
+ return -1;
}
}
@@ -192,11 +202,12 @@ Program::ExecuteAndWait(const Path& path,
// Wait for child to die
if (wait(&status) != child)
- ThrowErrno("Child timedout but wouldn't die");
+ MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
return -1; // Timeout detected
} else {
- ThrowErrno("Error waiting for child process");
+ MakeErrMsg(ErrMsg, "Error waiting for child process");
+ return -1;
}
// We exited normally without timeout, so turn off the timer.
@@ -223,12 +234,14 @@ Program::ExecuteAndWait(const Path& path,
}
-void Program::ChangeStdinToBinary(){
+bool Program::ChangeStdinToBinary(){
// Do nothing, as Unix doesn't differentiate between text and binary.
+ return false;
}
-void Program::ChangeStdoutToBinary(){
+bool Program::ChangeStdoutToBinary(){
// Do nothing, as Unix doesn't differentiate between text and binary.
+ return false;
}
}
diff --git a/lib/System/Unix/Unix.h b/lib/System/Unix/Unix.h
index 4ef3896..1516d45 100644
--- a/lib/System/Unix/Unix.h
+++ b/lib/System/Unix/Unix.h
@@ -119,4 +119,35 @@ inline void ThrowErrno(const std::string& prefix, int errnum = -1) {
throw prefix + ": " + buffer;
}
+/// This function builds an error message into \p ErrMsg using the \p prefix
+/// string and the Unix error number given by \p errnum. If errnum is -1, the
+/// default then the value of errno is used.
+/// @brief Make an error message
+inline void MakeErrMsg(
+ std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
+ if (!ErrMsg)
+ return;
+ char buffer[MAXPATHLEN];
+ buffer[0] = 0;
+ if (errnum == -1)
+ errnum = errno;
+#ifdef HAVE_STRERROR_R
+ // strerror_r is thread-safe.
+ if (errnum)
+ strerror_r(errnum,buffer,MAXPATHLEN-1);
+#elif HAVE_STRERROR
+ // Copy the thread un-safe result of strerror into
+ // the buffer as fast as possible to minimize impact
+ // of collision of strerror in multiple threads.
+ if (errnum)
+ strncpy(buffer,strerror(errnum),MAXPATHLEN-1);
+ buffer[MAXPATHLEN-1] = 0;
+#else
+ // Strange that this system doesn't even have strerror
+ // but, oh well, just use a generic message
+ sprintf(buffer, "Error #%d", errnum);
+#endif
+ *ErrMsg = buffer;
+}
+
#endif
diff --git a/lib/System/Win32/Program.inc b/lib/System/Win32/Program.inc
index c29adf0..b45cad1 100644
--- a/lib/System/Win32/Program.inc
+++ b/lib/System/Win32/Program.inc
@@ -69,7 +69,7 @@ Program::FindProgramByName(const std::string& progName) {
}
}
-static HANDLE RedirectIO(const Path *path, int fd) {
+static HANDLE RedirectIO(const Path *path, int fd, std::string* ErrMsg) {
HANDLE h;
if (path == 0) {
DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
@@ -91,8 +91,9 @@ static HANDLE RedirectIO(const Path *path, int fd) {
&sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) {
- ThrowError(std::string(fname) + ": Can't open file for " +
+ MakeErrMsg(ErrMsg, std::string(fname) + ": Can't open file for " +
(fd ? "input: " : "output: "));
+ return h;
}
return h;
}
@@ -102,9 +103,13 @@ Program::ExecuteAndWait(const Path& path,
const char** args,
const char** envp,
const Path** redirects,
- unsigned secondsToWait) {
- if (!path.canExecute())
- throw path.toString() + " is not executable";
+ unsigned secondsToWait,
+ std::string* ErrMsg) {
+ if (!path.canExecute()) {
+ if (ErrMsg)
+ *ErrMsg = "program not executable";
+ return -1;
+ }
// Windows wants a command line, not an array of args, to pass to the new
// process. We have to concatenate them all, while quoting the args that
@@ -148,21 +153,29 @@ Program::ExecuteAndWait(const Path& path,
if (redirects) {
si.dwFlags = STARTF_USESTDHANDLES;
- try {
- si.hStdInput = RedirectIO(redirects[0], 0);
- si.hStdOutput = RedirectIO(redirects[1], 1);
- if (redirects[1] && redirects[2] && *(redirects[1]) != *(redirects[2])) {
- si.hStdError = RedirectIO(redirects[2], 2);
- } else {
- DuplicateHandle(GetCurrentProcess(), si.hStdOutput,
- GetCurrentProcess(), &si.hStdError,
- 0, TRUE, DUPLICATE_SAME_ACCESS);
- }
- } catch (...) {
+ si.hStdInput = RedirectIO(redirects[0], 0, ErrMsg);
+ if (si.hStdInput == INVALID_HANDLE_VALUE) {
+ MakeErrMsg(ErrMsg, "can't redirect stdin");
+ return -1;
+ }
+ si.hStdOutput = RedirectIO(redirects[1], 1);
+ if (si.hStdOutput == INVALID_HANDLE_VALUE) {
CloseHandle(si.hStdInput);
- CloseHandle(si.hStdOutput);
- CloseHandle(si.hStdError);
- throw;
+ MakeErrMsg(ErrMsg, "can't redirect stdout");
+ return -1;
+ }
+ if (redirects[1] && redirects[2] && *(redirects[1]) != *(redirects[2])) {
+ si.hStdError = RedirectIO(redirects[2], 2);
+ if (si.hStdError == INVALID_HANDLE_VALUE) {
+ CloseHandle(si.hStdInput);
+ CloseHandle(si.hStdOutput);
+ MakeErrMsg(ErrMsg, "can't redirect stderr");
+ return -1;
+ }
+ } else {
+ DuplicateHandle(GetCurrentProcess(), si.hStdOutput,
+ GetCurrentProcess(), &si.hStdError,
+ 0, TRUE, DUPLICATE_SAME_ACCESS);
}
}
@@ -181,12 +194,13 @@ Program::ExecuteAndWait(const Path& path,
CloseHandle(si.hStdOutput);
CloseHandle(si.hStdError);
- // Now throw an error if the process didn't get created.
+ // Now return an error if the process didn't get created.
if (!rc)
{
SetLastError(err);
- ThrowError(std::string("Couldn't execute program '") +
+ MakeErrMsg(ErrMsg, std::string("Couldn't execute program '") +
path.toString() + "'");
+ return -1;
}
// Wait for it to terminate.
@@ -196,8 +210,9 @@ Program::ExecuteAndWait(const Path& path,
if (WaitForSingleObject(pi.hProcess, millisecondsToWait) == WAIT_TIMEOUT) {
if (!TerminateProcess(pi.hProcess, 1)) {
- ThrowError(std::string("Failed to terminate timed-out program '") +
- path.toString() + "'");
+ MakeErrMsg(ErrMsg, std::string("Failed to terminate timed-out program '")
+ + path.toString() + "'");
+ return -1;
}
WaitForSingleObject(pi.hProcess, INFINITE);
}
@@ -213,23 +228,22 @@ Program::ExecuteAndWait(const Path& path,
if (!rc) {
SetLastError(err);
- ThrowError(std::string("Failed getting status for program '") +
+ MakeErrMsg(ErrMsg, std::string("Failed getting status for program '") +
path.toString() + "'");
+ return -1;
}
return status;
}
-void Program::ChangeStdinToBinary(){
+bool Program::ChangeStdinToBinary(){
int result = _setmode( _fileno(stdin), _O_BINARY );
- if( result == -1 )
- throw std::string("Cannot set input mode on stdin to binary.");
+ return result == -1;
}
-void Program::ChangeStdoutToBinary(){
+bool Program::ChangeStdoutToBinary(){
int result = _setmode( _fileno(stdout), _O_BINARY );
- if( result == -1 )
- throw std::string("Cannot set output mode on stdout to binary.");
+ return result == -1;
}
}
diff --git a/lib/System/Win32/Win32.h b/lib/System/Win32/Win32.h
index ef2d66f..7243548 100644
--- a/lib/System/Win32/Win32.h
+++ b/lib/System/Win32/Win32.h
@@ -44,6 +44,20 @@ inline void ThrowError(const std::string& msg) {
throw s;
}
+inline void MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
+ if (!ErrMsg)
+ return;
+ char *buffer = NULL;
+ FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
+ NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
+ ErrMsg = prefix + buffer;
+ LocalFree(buffer);
+}
+
inline void ThrowErrno(const std::string& prefix) {
- ThrowError(prefix + ": " + strerror(errno));
+ ThrowError(prefix + ": " + strerror(errno));
+}
+
+inline void MakeErrnoMsg(std::string* ErrMsg, const std::string & prefix) {
+ MakeErrorMsg(prefix + ": " + strerror(errno));
}