Commit c24fbfd7 authored by Mark OLESEN's avatar Mark OLESEN Committed by Andrew Heather
Browse files

ENH: improve code isolation for signal-handling

- file-local static for saving the old action, which moves system
  dependencies out of the header files.

- set/reset of signals as file-local functions

STYLE: use csignal header instead of signal.h
parent 36fae9fd
......@@ -3,14 +3,16 @@ clockValue/clockValue.C
cpuInfo/cpuInfo.C
cpuTime/cpuTime.C
memInfo/memInfo.C
signals/sigFpe.C
signals/sigSegv.C
signals/sigInt.C
signals/sigQuit.C
signals/sigStopAtWriteNow.C
signals/sigWriteNow.C
signals/timer.C
regExpPosix.C
timer.C
fileStat.C
POSIX.C
......
......@@ -41,7 +41,7 @@ SourceFiles
#define cpuTime_H
#include <string>
#include <time.h>
#include <ctime>
#include <sys/times.h>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2015 OpenFOAM Foundation
......@@ -54,6 +54,7 @@ SourceFiles
namespace Foam
{
// Forward Declarations
class fileMonitor;
class fileMonitorWatcher;
......@@ -63,7 +64,6 @@ class fileMonitorWatcher;
class fileMonitor
{
public:
// Public data types
......@@ -79,6 +79,7 @@ public:
static const Enum<fileState> fileStateNames_;
private:
// Private data
//- Whether to use inotify (requires -DFOAM_USE_INOTIFY, see above)
......
......@@ -29,7 +29,6 @@ License
#include "IOstreams.H"
#include "timer.H"
#include <signal.h>
#include <unistd.h>
#ifndef darwin
#include <sys/sysmacros.h>
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2015 OpenFOAM Foundation
......@@ -33,6 +33,8 @@ License
#include "Switch.H"
#include "UList.H"
#include <limits>
#if defined(__linux__) && defined(__GNUC__)
#ifndef __USE_GNU
#define __USE_GNU // To use feenableexcept()
......@@ -47,7 +49,9 @@ License
#include "feexceptErsatz.H"
#endif
#include <limits>
// File-local functions
#include "signalMacros.C"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
......@@ -57,8 +61,6 @@ bool Foam::sigFpe::switchNan_(Foam::debug::optimisationSwitch("setNaN", 0));
bool Foam::sigFpe::sigActive_ = false;
bool Foam::sigFpe::nanActive_ = false;
struct sigaction Foam::sigFpe::oldAction_;
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
......@@ -123,17 +125,11 @@ void Foam::sigFpe::sigHandler(int)
{
#if (defined(__linux__) && defined(__GNUC__)) || defined(darwin)
// Reset old handling
if (sigaction(SIGFPE, &oldAction_, nullptr) < 0)
{
FatalErrorInFunction
<< "Cannot reset SIGFPE trapping"
<< abort(FatalError);
}
resetHandler("SIGFPE", SIGFPE);
jobInfo.signalEnd(); // Update jobInfo file
error::printStack(Perr);
raise(SIGFPE); // Throw signal (to old handler)
::raise(SIGFPE); // Throw signal (to old handler)
#endif // (__linux__ && __GNUC__) || darwin
}
......@@ -203,17 +199,7 @@ void Foam::sigFpe::set(bool verbose)
| FE_OVERFLOW
);
struct sigaction newAction;
newAction.sa_handler = sigHandler;
newAction.sa_flags = SA_NODEFER;
sigemptyset(&newAction.sa_mask);
if (sigaction(SIGFPE, &newAction, &oldAction_) < 0)
{
FatalErrorInFunction
<< "Cannot set SIGFPE trapping"
<< abort(FatalError);
}
setHandler("SIGFPE", SIGFPE, sigHandler);
sigActive_ = true;
......@@ -293,12 +279,7 @@ void Foam::sigFpe::unset(bool verbose)
<< endl;
}
if (sigaction(SIGFPE, &oldAction_, nullptr) < 0)
{
FatalErrorInFunction
<< "Cannot reset SIGFPE trapping"
<< abort(FatalError);
}
resetHandler("SIGFPE", SIGFPE);
// Reset exception raising
const int oldExcept = fedisableexcept
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -56,7 +56,6 @@ SourceFiles
#ifndef sigFpe_H
#define sigFpe_H
#include <signal.h>
#include "scalar.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -64,7 +63,7 @@ SourceFiles
namespace Foam
{
// Forward declarations
// Forward Declarations
template<class T> class UList;
/*---------------------------------------------------------------------------*\
......@@ -73,7 +72,7 @@ template<class T> class UList;
class sigFpe
{
// Private data
// Private Data
//- Flag that floating point trapping should be used.
// Can override with FOAM_SIGFPE env variable
......@@ -83,20 +82,16 @@ class sigFpe
// Can override with FOAM_SETNAN env variable
static bool switchNan_;
//- Flag to indicate floating point trapping is currently active
//- Floating point trapping currently active?
static bool sigActive_;
//- Flag to indicate mallocNan is currently active
static bool nanActive_;
//- Saved old signal trapping setting
static struct sigaction oldAction_;
// Private Member Functions
//- Handler for caught signals.
// Ends job and prints stack
//- Handler for caught signals - ends job and prints stack
static void sigHandler(int);
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2015 OpenFOAM Foundation
......@@ -30,9 +30,11 @@ License
#include "JobInfo.H"
#include "IOstreams.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
// File-local functions
#include "signalMacros.C"
struct sigaction Foam::sigInt::oldAction_;
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
bool Foam::sigInt::sigActive_ = false;
......@@ -41,16 +43,10 @@ bool Foam::sigInt::sigActive_ = false;
void Foam::sigInt::sigHandler(int)
{
// Reset old handling
if (sigaction(SIGINT, &oldAction_, nullptr) < 0)
{
FatalErrorInFunction
<< "Cannot reset SIGINT trapping"
<< abort(FatalError);
}
resetHandler("SIGINT", SIGINT);
jobInfo.signalEnd(); // Update jobInfo file
raise(SIGINT); // Throw signal (to old handler)
::raise(SIGINT); // Throw signal (to old handler)
}
......@@ -74,35 +70,25 @@ Foam::sigInt::~sigInt()
void Foam::sigInt::set(bool)
{
if (!sigActive_)
if (sigActive_)
{
struct sigaction newAction;
newAction.sa_handler = sigHandler;
newAction.sa_flags = SA_NODEFER;
sigemptyset(&newAction.sa_mask);
if (sigaction(SIGINT, &newAction, &oldAction_) < 0)
{
FatalErrorInFunction
<< "Cannot call sigInt::set() more than once"
<< abort(FatalError);
}
sigActive_ = true;
return;
}
sigActive_ = true;
setHandler("SIGINT", SIGINT, sigHandler);
}
void Foam::sigInt::unset(bool)
{
if (sigActive_)
if (!sigActive_)
{
if (sigaction(SIGINT, &oldAction_, nullptr) < 0)
{
FatalErrorInFunction
<< "Cannot set SIGINT trapping"
<< abort(FatalError);
}
sigActive_ = false;
return;
}
sigActive_ = false;
resetHandler("SIGINT", SIGINT);
}
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -47,8 +47,6 @@ SourceFiles
#ifndef sigInt_H
#define sigInt_H
#include <signal.h>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
......@@ -62,10 +60,7 @@ class sigInt
{
// Private data
//- Saved old signal trapping setting
static struct sigaction oldAction_;
//- Flag to indicate signal trapping is enabled
//- Signal trapping enabled?
static bool sigActive_;
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2015 OpenFOAM Foundation
......@@ -30,9 +30,11 @@ License
#include "JobInfo.H"
#include "IOstreams.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
// File-local functions
#include "signalMacros.C"
struct sigaction Foam::sigQuit::oldAction_;
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
bool Foam::sigQuit::sigActive_ = false;
......@@ -41,17 +43,11 @@ bool Foam::sigQuit::sigActive_ = false;
void Foam::sigQuit::sigHandler(int)
{
// Reset old handling
if (sigaction(SIGQUIT, &oldAction_, nullptr) < 0)
{
FatalErrorInFunction
<< "Cannot reset SIGQUIT trapping"
<< abort(FatalError);
}
resetHandler("SIGQUIT", SIGQUIT);
jobInfo.signalEnd(); // Update jobInfo file
error::printStack(Perr);
raise(SIGQUIT); // Throw signal (to old handler)
::raise(SIGQUIT); // Throw signal (to old handler)
}
......@@ -75,35 +71,25 @@ Foam::sigQuit::~sigQuit()
void Foam::sigQuit::set(bool)
{
if (!sigActive_)
if (sigActive_)
{
struct sigaction newAction;
newAction.sa_handler = sigHandler;
newAction.sa_flags = SA_NODEFER;
sigemptyset(&newAction.sa_mask);
if (sigaction(SIGQUIT, &newAction, &oldAction_) < 0)
{
FatalErrorInFunction
<< "Cannot call more than once"
<< abort(FatalError);
}
sigActive_ = true;
return;
}
sigActive_ = true;
setHandler("SIGQUIT", SIGQUIT, sigHandler);
}
void Foam::sigQuit::unset(bool)
{
if (sigActive_)
if (!sigActive_)
{
if (sigaction(SIGQUIT, &oldAction_, nullptr) < 0)
{
FatalErrorInFunction
<< "Cannot unset SIGQUIT trapping"
<< abort(FatalError);
}
sigActive_ = false;
return;
}
sigActive_ = false;
resetHandler("SIGQUIT", SIGQUIT);
}
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -46,8 +46,6 @@ SourceFiles
#ifndef sigQuit_H
#define sigQuit_H
#include <signal.h>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
......@@ -59,12 +57,9 @@ namespace Foam
class sigQuit
{
// Private data
//- Saved old signal trapping setting
static struct sigaction oldAction_;
// Private Data
//- Flag to indicate signal trapping is enabled
//- Signal trapping enabled?
static bool sigActive_;
......@@ -86,7 +81,7 @@ public:
~sigQuit();
// Member functions
// Member Functions
//- Activate SIGQUIT signal handler
static void set(bool verbose=false);
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2015 OpenFOAM Foundation
......@@ -30,9 +30,11 @@ License
#include "JobInfo.H"
#include "IOstreams.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
// File-local functions
#include "signalMacros.C"
struct sigaction Foam::sigSegv::oldAction_;
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
bool Foam::sigSegv::sigActive_ = false;
......@@ -41,17 +43,11 @@ bool Foam::sigSegv::sigActive_ = false;
void Foam::sigSegv::sigHandler(int)
{
// Reset old handling
if (sigaction(SIGSEGV, &oldAction_, nullptr) < 0)
{
FatalErrorInFunction
<< "Cannot reset SIGSEGV trapping"
<< abort(FatalError);
}
resetHandler("SIGSEGV", SIGSEGV);
jobInfo.signalEnd(); // Update jobInfo file
error::printStack(Perr);
raise(SIGSEGV); // Throw signal (to old handler)
::raise(SIGSEGV); // Throw signal (to old handler)
}
......@@ -75,35 +71,25 @@ Foam::sigSegv::~sigSegv()
void Foam::sigSegv::set(bool)
{
if (!sigActive_)
if (sigActive_)
{
struct sigaction newAction;
newAction.sa_handler = sigHandler;
newAction.sa_flags = SA_NODEFER;
sigemptyset(&newAction.sa_mask);
if (sigaction(SIGSEGV, &newAction, &oldAction_) < 0)
{
FatalErrorInFunction
<< "Cannot call more than once"
<< abort(FatalError);
}
sigActive_ = true;
return;
}
sigActive_ = true;
setHandler("SIGSEGV", SIGSEGV, sigHandler);
}
void Foam::sigSegv::unset(bool)
{
if (sigActive_)
if (!sigActive_)
{
if (sigaction(SIGSEGV, &oldAction_, nullptr) < 0)
{
FatalErrorInFunction
<< "Cannot unset SIGSEGV trapping"
<< abort(FatalError);
}
sigActive_ = false;
return;
}
sigActive_ = false;
resetHandler("SIGSEGV", SIGSEGV);
}
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -46,8 +46,6 @@ SourceFiles
#ifndef sigSegv_H
#define sigSegv_H
#include <signal.h>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
......@@ -59,12 +57,9 @@ namespace Foam
class sigSegv
{
// Private data
//- Saved old signal trapping setting
static struct sigaction oldAction_;
// Private Data
//- Flag to indicate signal trapping is enabled
//- Signal trapping enabled?
static bool sigActive_;
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -25,12 +25,17 @@ License
\*---------------------------------------------------------------------------*/
#include "sigWriteNow.H"
#include "sigStopAtWriteNow.H"
#include "error.H"
#include "JobInfo.H"
#include "IOstreams.H"
#include "Time.H"
// File-local functions
#include "signalMacros.C"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
// Signal number to catch
......@@ -39,21 +44,19 @@ int Foam::sigStopAtWriteNow::signal_
Foam::debug::optimisationSwitch("stopAtWriteNowSignal", -1)
);
Foam::Time const* Foam::sigStopAtWriteNow::runTimePtr_ = nullptr;
// Pointer to Time (file-local variable)
static Foam::Time const* runTimePtr_ = nullptr;
struct sigaction Foam::sigStopAtWriteNow::oldAction_;
// * * * * * * * * * * * * * * * Local Classes * * * * * * * * * * * * * * * //
namespace Foam
{
// Register re-reader
class addstopAtWriteNowSignalToOpt
struct addstopAtWriteNowSignalToOpt
:
public ::Foam::simpleRegIOobject
{