Source
SIMinorCycleController::useSmallSummaryminor() ? 6 : SIMinorCycleController::nSummaryFields, // temporary CAS-13683 workaround
extern "C" char **environ;
using namespace casacore;
// https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
// C++ is so ridiculous... trim from start (in place)
static inline void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
return !std::isspace(ch);
}));
}
// trim from end (in place)
static inline void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) {
return !std::isspace(ch);
}).base(), s.end());
}
// trim from both ends (in place)
static inline void trim(std::string &s) {
ltrim(s);
rtrim(s);
}
// Executes the given program, with the given arguments and the given environment.
// The stdout from the program is collected and returned in output, up to outputlen characters.
// @param envp To get around the MPI issue from CAS-13252, this should probably come from getenv_sansmpi().
static void execve_getstdout(char *pathname, char *argv[], char *envp[], char *output, ssize_t outputlen)
{
// We use execve here instead of popen to get around issues related to using MPI.
// MPI crashes when starting a process that calls MPI_Init in a process spawned using popen or exec.
// We can trick MPI into behaving itself by removing all the MPI environmental variables for
// the child precess (thus getenv_sansmpi and execve).
int filedes[2];
if (pipe(filedes) == -1) {
std::cerr << "pipe error" << std::endl;
exit(1);
}
pid_t pid = fork();
if (pid == -1) {
std::cerr << "fork error" << std::endl;
exit(1);
} else if (pid == 0) { // child
// close stdout and connect it to the input of the pipe
while ((dup2(filedes[1], STDOUT_FILENO) == -1) && (errno == EINTR)) {}
close(filedes[1]);
close(filedes[0]);
// exec on the child process
execve(pathname, argv, envp);
exit(1);
} else { // parent
// don't care about the input end of the pipe
close(filedes[1]);
const ssize_t tmplen = 128;