+ void cmdRunOnThreadWithFork (TestStepCmd *step, const std::string &cmd) {
+
+ // Thread running:
+ step->setThreadRunning(true);
+
+ pid_t cpid, w;
+ int status = -2;
+
+ if ((cpid = fork()) < 0) {
+ step->setErrorMsg("Error in fork()");
+ }
+ else if (cpid == 0) {
+ // child
+ status = system(cmd.c_str());
+ _exit(WEXITSTATUS(status));
+ }
+ else {
+ // parent
+ step->setChildPid(cpid);
+ do {
+ w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
+ if (w != -1) {
+
+ if (WIFEXITED(status)) {
+ step->setResultCode(WEXITSTATUS(status)); // rc = status >>= 8; // divide by 256
+ break;
+ }
+ else if (WIFSIGNALED(status)) {
+ step->setErrorMsg(anna::functions::asString("killed by signal %d", WTERMSIG(status)));
+ step->setResultCode(128 + WTERMSIG(status));
+ break;
+ } else if (WIFSTOPPED(status)) {
+ step->setErrorMsg(anna::functions::asString("stopped by signal %d", WSTOPSIG(status)));
+ } else if (WIFCONTINUED(status)) {
+ step->setErrorMsg("continued");
+ }
+ }
+ else {
+ step->setErrorMsg("waitpid error");
+ step->setResultCode(-1);
+ break;
+ }
+ } while (!WIFEXITED(status) && !WIFSIGNALED(status));
+
+ step->complete();
+ }
+ }
+