1 #include <system_error>
6 #include <sys/socket.h>
27 throw std::runtime_error(cmd +
" is done - cannot check open fd count");
33 auto dirp = opendir(path.c_str());
34 if (dirp ==
nullptr) {
35 throw std::system_error(
errno, std::generic_category(),
"opendir error: " + path);
38 for(
auto dp = readdir(dirp); dp !=
nullptr; dp = readdir(dirp)) {
39 std::string name = dp->d_name;
40 if (name.length() == 0 || name[0] ==
'.') {
55 if (count != expected) {
67 throw std::runtime_error(cmd +
" already running");
73 argCount = args.size();
74 const char * fullArgs[argCount + 2];
75 fullArgs[0] = path.c_str();
76 for(
int i = 0; i < argCount ; i++) {
77 fullArgs[i + 1] = args[i].c_str();
79 fullArgs[1 + argCount] =
nullptr;
80 fprintf(stderr,
"Running ");
81 for(
int i = 0; i < 2+ argCount ; ++i) {
82 fprintf(stderr,
" %s", fullArgs[i] ? fullArgs[i] :
"<null>");
84 fprintf(stderr,
"\n");
88 throw std::system_error(
errno, std::generic_category(),
"fork error");
91 std::string error =
"exec " + path;
93 execv(fullArgs[0], (
char *
const *) fullArgs);
96 perror(error.c_str());
117 pid_t waited = waitpid(pid, &status, 0);
119 throw std::system_error(
errno, std::generic_category(),
"waitpid error");
129 pid_t waited = waitpid(pid, &status, WNOHANG);
131 throw std::system_error(
errno, std::generic_category(),
"waitpid error");
135 throw std::runtime_error(
"Process " + cmd +
" not done");
143 pid_t waited = waitpid(pid, &status, WNOHANG);
145 throw std::system_error(
errno, std::generic_category(),
"waitpid error");
154 throw std::runtime_error(
"Process terminated unexpectedly");
160 if (!WIFEXITED(status)) {
161 if (WIFSIGNALED(status)) {
162 throw std::runtime_error(cmd +
" got signal " + strsignal(WTERMSIG(status)));
165 throw std::runtime_error(cmd +
" exited abnormaly");
167 int actual = WEXITSTATUS(status);
void expectExitCode(int e)
void start(const std::string &path, const std::vector< std::string > &args)
void checkOpenFdCount(int expected, const std::string &msg)
void waitProcessEnd(int expectedExitCode)
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values