sigQuit.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "sigQuit.H"
27 #include "error.H"
28 #include "JobInfo.H"
29 #include "IOstreams.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 struct sigaction Foam::sigQuit::oldAction_;
34 
35 bool Foam::sigQuit::sigActive_ = false;
36 
37 
38 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
39 
41 {
42  // Reset old handling
43  if (sigaction(SIGQUIT, &oldAction_, NULL) < 0)
44  {
46  << "Cannot reset SIGQUIT trapping"
47  << abort(FatalError);
48  }
49 
50  // Update jobInfo file
52 
53  error::printStack(Perr);
54 
55  // Throw signal (to old handler)
56  raise(SIGQUIT);
57 }
58 
59 
60 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
61 
63 {
64  set(false);
65 }
66 
67 
68 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
69 
71 {
72  unset(false);
73 }
74 
75 
76 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
77 
78 void Foam::sigQuit::set(const bool verbose)
79 {
80  if (!sigActive_)
81  {
82  struct sigaction newAction;
83  newAction.sa_handler = sigHandler;
84  newAction.sa_flags = SA_NODEFER;
85  sigemptyset(&newAction.sa_mask);
86  if (sigaction(SIGQUIT, &newAction, &oldAction_) < 0)
87  {
89  << "Cannot call sigQuit::set() more than once"
90  << abort(FatalError);
91  }
92  sigActive_ = true;
93  }
94 }
95 
96 
97 void Foam::sigQuit::unset(const bool)
98 {
99  if (sigActive_)
100  {
101  if (sigaction(SIGQUIT, &oldAction_, NULL) < 0)
102  {
104  << "Cannot set SIGQUIT trapping"
105  << abort(FatalError);
106  }
107  sigActive_ = false;
108  }
109 }
110 
111 
112 // ************************************************************************* //
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::sigQuit::unset
static void unset(const bool verbose)
Deactivate SIGQUIT signal handler.
Definition: sigQuit.C:97
Foam::Perr
prefixOSstream Perr(cerr, "Perr")
Definition: IOstreams.H:54
Foam::sigQuit::~sigQuit
~sigQuit()
Destructor.
Definition: sigQuit.C:70
error.H
Foam::sigQuit::set
static void set(const bool verbose)
Activate SIGQUIT signal handler.
Definition: sigQuit.C:78
Foam::sigQuit::sigActive_
static bool sigActive_
Flag to indicate signal trapping is enabled.
Definition: sigQuit.H:65
Foam::FatalError
error FatalError
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::sigQuit::sigHandler
static void sigHandler(int)
Handler for caught signals.
Definition: sigQuit.C:40
Foam::jobInfo
JobInfo jobInfo
Definition: JobInfo.C:35
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
JobInfo.H
Foam::JobInfo::signalEnd
void signalEnd() const
Definition: JobInfo.C:176
sigQuit.H
Foam::sigQuit::sigQuit
sigQuit()
Construct null.
Definition: sigQuit.C:62