27 throw std::runtime_error(cmd +
" is done - cannot check open fd count");
30 std::string path =
"/proc/" + std::to_string(pid) +
"/fd";
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] ==
'.') {
67 throw std::runtime_error(cmd +
" already running");
73 argCount = args.size();
74 std::vector<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.data());
96 perror(error.c_str());