Commit 57a248f3 authored by Jason Rhinelander's avatar Jason Rhinelander
Browse files

Undo previous commit; change to unbuffered

The previous commit didn't fix things--some stdout could still be lost.
This is another attempt to fix the problem by making the log file
unbuffered.
parent e994d49d
Pipeline #99 skipped
......@@ -275,10 +275,19 @@ 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 + "'");
}
setbuf(LOGFILE, nullptr);
}
if (first) { first = false; started.set_value(); }
std::chrono::high_resolution_clock::time_point started;
pid_t child;
......@@ -316,16 +325,11 @@ 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) {
dup2(fileno(LOGFILE), STDOUT_FILENO);
dup2(fileno(LOGFILE), STDERR_FILENO);
close(fileno(LOGFILE));
close(STDIN_FILENO);
// Restore the previously-blocked SIGINT so that ssh can be interrupted
pthread_sigmask(SIG_UNBLOCK, &sigint, NULL);
......@@ -342,7 +346,6 @@ 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 {
......@@ -404,6 +407,8 @@ 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