#include <ctime>
#include <unistd.h>
#include <poll.h>
+#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
+#include <sys/stat.h>
#include <dpl/utils/wrt_global_settings.h>
namespace {
const int PIPE_CLOSED = -1;
+
+const int CHILD_TEST_FAIL = 0;
+const int CHILD_TEST_PASS = 1;
+const int CHILD_TEST_IGNORED = 2;
+
+int closeOutput() {
+ int devnull;
+ int retcode = -1;
+ if (-1 == (devnull = TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY))))
+ return -1;
+
+ // replace stdout with /dev/null
+ if (-1 == TEMP_FAILURE_RETRY(dup2(devnull, STDOUT_FILENO)))
+ goto end;
+
+ // replace stderr with /dev/null
+ if (-1 == TEMP_FAILURE_RETRY(dup2(devnull, STDERR_FILENO)))
+ goto end;
+
+ retcode = 0;
+
+end:
+ close(devnull);
+ return retcode;
}
+} // namespace anonymous
+
namespace DPL {
namespace Test {
class PipeWrapper : DPL::Noncopyable
throw TestRunner::TestFailed("Reading pipe error");
}
- if (code == 0) {
+ if (code == CHILD_TEST_FAIL) {
throw TestRunner::TestFailed(message);
- }
- if (code == 2) {
- throw TestRunner::TestFailed("Ignored");
+ } else if (code == CHILD_TEST_IGNORED) {
+ throw TestRunner::Ignored(message);
}
} else {
// child code
// End Runner after current test
TestRunnerSingleton::Instance().Terminate();
- int code = 1;
+ int code = CHILD_TEST_PASS;
std::string msg;
bool allowLogs = TestRunnerSingleton::Instance().GetAllowChildLogs();
- close(0); // stdin
+ close(STDIN_FILENO);
if (!allowLogs) {
- close(1); // stdout
- close(2); // stderr
+ closeOutput(); // if fails nothing we can do
}
pipe.setUsage(PipeWrapper::WRITEONLY);
try {
procChild();
- } catch (DPL::Test::TestRunner::TestFailed &e) {
+ } catch (const DPL::Test::TestRunner::TestFailed &e) {
msg = e.GetMessage();
- code = 0;
- } catch (DPL::Test::TestRunner::Ignored &e) {
+ code = CHILD_TEST_FAIL;
+ } catch (const DPL::Test::TestRunner::Ignored &e) {
msg = e.GetMessage();
- code = 2;
- } catch (...) { // Pokemon Catch... cache them all...
+ code = CHILD_TEST_IGNORED;
+ } catch (...) { // catch all exception generated by "user" code
msg = "unhandled exeception";
- code = 0;
+ code = CHILD_TEST_FAIL;
}
if (allowLogs) {
- close(1); // stdout
- close(2); // stderr
+ closeOutput();
}
pipe.send(code, msg);