Ignore ECHILD during system call on thread
[anna.git] / example / diameter / launcher / testing / TestStep.cpp
index 605407e..b1da8fb 100644 (file)
@@ -10,6 +10,7 @@
 #include <string>
 #include <iostream>
 #include <stdio.h>
+#include <errno.h>
 
 // Project
 #include <anna/xml/Compiler.hpp>
@@ -34,7 +35,21 @@ namespace {
   void cmdRunOnThread (TestStepCmd *step, const std::string &cmd) {
     step->setThreadRunning(true);
     int rc = system(cmd.c_str());
-    if (rc != -1) rc >>= 8; // divide by 256
+    if (rc < 0 && errno == ECHILD) rc = 0; // ignore, it could happens
+    // I know one reason for this is that SICCHLD is set to SIG_IGN but this
+    // should not be the case here. SIGCHLD is explicity set to SIG_DFL
+    // using a sigaction before the call to system(). (Although it is
+    // normally set to SIG_IGN). There should not be any other threads
+    // messing about with SIGCHLD.
+
+    if (rc < 0) {
+      step->setErrorMsg(anna::functions::asString("errno = %d", errno));
+      //std::terminate;
+    }
+    else {
+      rc >>= 8; // divide by 256
+    }
+
     step->setResultCode(rc);
     step->complete();
     // TODO: timeout the system call
@@ -422,6 +437,7 @@ throw() {
   result->createAttribute("Script", (a_script != "") ? a_script:"<no script>");
   result->createAttribute("Parameters", (a_parameters != "") ? a_parameters:"<no parameters>");
   result->createAttribute("CommandInProgress", a_threadRunning ? "yes":"no");
+  if (a_errorMsg != "") result->createAttribute("ErrorMessage", a_errorMsg);
   if (!a_threadRunning && a_resultCode != -2) {
     result->createAttribute("ResultCode", a_resultCode);
     //if (a_output != "") result->createAttribute("Output", a_output);