move assignment of signal handlers into utility function in terminal.cpp

This commit is contained in:
Arne Morten Kvarving 2024-09-05 13:45:12 +02:00
parent 7726924cb1
commit 929515c672
3 changed files with 21 additions and 13 deletions

View File

@ -48,9 +48,7 @@
#include <sstream>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#if HAVE_MPI
#include <mpi.h>
@ -206,17 +204,7 @@ static inline int start(int argc, char **argv, bool registerParams=true)
using Problem = GetPropType<TypeTag, Properties::Problem>;
using TM = GetPropType<TypeTag, Properties::ThreadManager>;
// set the signal handlers to reset the TTY to a well defined state on unexpected
// program aborts
if (isatty(STDIN_FILENO)) {
signal(SIGINT, resetTerminal);
signal(SIGHUP, resetTerminal);
signal(SIGABRT, resetTerminal);
signal(SIGFPE, resetTerminal);
signal(SIGSEGV, resetTerminal);
signal(SIGPIPE, resetTerminal);
signal(SIGTERM, resetTerminal);
}
assignResetTerminalSignalHandlers();
resetLocale();

View File

@ -105,6 +105,21 @@ int getTtyWidth()
return ttyWidth;
}
void assignResetTerminalSignalHandlers()
{
// set the signal handlers to reset the TTY to a well defined state on unexpected
// program aborts
if (isatty(STDIN_FILENO)) {
signal(SIGINT, resetTerminal);
signal(SIGHUP, resetTerminal);
signal(SIGABRT, resetTerminal);
signal(SIGFPE, resetTerminal);
signal(SIGSEGV, resetTerminal);
signal(SIGPIPE, resetTerminal);
signal(SIGTERM, resetTerminal);
}
}
void resetTerminal()
{
// make sure stderr and stderr do not contain any unwritten data and make sure that

View File

@ -44,6 +44,11 @@ std::string breakLines(const std::string& msg,
*/
int getTtyWidth();
/*!
* \brief Assign signal handlers that reset the terminal on errors.
*/
void assignResetTerminalSignalHandlers();
/*!
* \brief Resets the current TTY to a usable state if the program was aborted.
*