1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#ifndef APPLICATION_H
#define APPLICATION_H
#include <aocommon/logger.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
namespace wsclean {
class Application {
public:
static void Run(const std::string& commandLine) {
aocommon::Logger::Info << "Running: " << commandLine << '\n';
const char* commandLineCStr = commandLine.c_str();
int pid = vfork();
switch (pid) {
case -1: // Error
throw std::runtime_error(
"Could not vfork() new process for executing command line "
"application");
case 0: // Child
execl("/bin/sh", "sh", "-c", commandLineCStr, NULL);
_exit(127);
}
// Wait for process to terminate
int pStatus;
do {
int pidReturn;
do {
pidReturn = waitpid(pid, &pStatus, 0);
} while (pidReturn == -1 && errno == EINTR);
} while (!WIFEXITED(pStatus) && !WIFSIGNALED(pStatus));
if (WIFEXITED(pStatus)) {
// all good
// const int exitStatus = WEXITSTATUS(pStatus);
} else {
throw std::runtime_error(
"Running command line application returned an error");
}
}
};
} // namespace wsclean
#endif
|