Commit e994d49d authored by Jason Rhinelander's avatar Jason Rhinelander
Browse files

Close and reopen log file for each job

Otherwise there is potential conflict since the parent and child both
have the file open--but this can cause data loss, since they'll not
have the same file position after the fork.

So reopen for each job, and close (in the parent) immediately after the
fork.
parent 503fec9a
Pipeline #98 skipped
......@@ -275,18 +275,10 @@ void thread_runner(std::promise<void> started, const std::pair<std::string, std:
const std::string hostthr = hostname + ":" + std::to_string(threadnum);
long myjobno;
bool first = true;
FILE *LOGFILE = nullptr;
sigset_t sigint; sigaddset(&sigint, SIGINT);
std::string log_file = log_dir + "/" + hostname + "-" + std::to_string(threadnum) + ".log";
while (not abort_jobs and (myjobno = ++jobno) <= total_jobs) {
if (!LOGFILE) {
std::string log_file = log_dir + "/" + hostname + "-" + std::to_string(threadnum) + ".log";
LOGFILE = fopen(log_file.c_str(), "a");
if (!LOGFILE) {
throw std::system_error(errno, std::system_category(), "Unable to open log file '" + log_file + "'");
}
}
if (first) { first = false; started.set_value(); }
std::chrono::high_resolution_clock::time_point started;
pid_t child;
......@@ -324,6 +316,10 @@ void thread_runner(std::promise<void> started, const std::pair<std::string, std:
"trap \"kill -HUP \\$subproc; pkill -HUP -P \\$subproc\" HUP; "
"wait"; // Waits for child to finish
FILE *LOGFILE = fopen(log_file.c_str(), "a");
if (!LOGFILE) {
throw std::system_error(errno, std::system_category(), "Unable to open log file '" + log_file + "'");
}
const char execfailed[] = "Exec /usr/bin/ssh failed";
std::string host_connect = username.empty() ? hostname : username + "@" + hostname;
if ((child = fork()) == 0) {
......@@ -346,6 +342,7 @@ void thread_runner(std::promise<void> started, const std::pair<std::string, std:
write(STDERR_FILENO, execfailed, sizeof(execfailed));
}
else if (child != 0) {
fclose(LOGFILE);
ssh_pids.insert(child);
}
else {
......@@ -407,8 +404,6 @@ void thread_runner(std::promise<void> started, const std::pair<std::string, std:
}
// Reply to the promise if we didn't get to do anything
if (first) started.set_value();
if (LOGFILE) fclose(LOGFILE);
}
// Returns the maximum jobs at once on the given host. If the second argument
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment