All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sigQuit.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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 
36 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
37 
38 void Foam::sigQuit::sigHandler(int)
39 {
40  // Reset old handling
41  if (sigaction(SIGQUIT, &oldAction_, nullptr) < 0)
42  {
44  << "Cannot reset SIGQUIT trapping"
45  << abort(FatalError);
46  }
47 
48  // Update jobInfo file
50 
51  error::printStack(Perr);
52 
53  // Throw signal (to old handler)
54  raise(SIGQUIT);
55 }
56 
57 
58 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
59 
61 {
62  oldAction_.sa_handler = nullptr;
63 }
64 
65 
66 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
67 
69 {
70  // Reset old handling
71  if (oldAction_.sa_handler && sigaction(SIGQUIT, &oldAction_, nullptr) < 0)
72  {
74  << "Cannot reset SIGQUIT trapping"
75  << abort(FatalError);
76  }
77 }
78 
79 
80 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
81 
82 void Foam::sigQuit::set(const bool verbose)
83 {
84  if (oldAction_.sa_handler)
85  {
87  << "Cannot call sigQuit::set() more than once"
88  << abort(FatalError);
89  }
90 
91  struct sigaction newAction;
92  newAction.sa_handler = sigHandler;
93  newAction.sa_flags = SA_NODEFER;
94  sigemptyset(&newAction.sa_mask);
95  if (sigaction(SIGQUIT, &newAction, &oldAction_) < 0)
96  {
98  << "Cannot set SIGQUIT trapping"
99  << abort(FatalError);
100  }
101 }
102 
103 
104 // ************************************************************************* //
void set(const bool verbose)
Activate SIGQUIT signal handler.
Definition: sigQuit.C:82
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
void signalEnd() const
Definition: jobInfo.C:210
jobInfo jobInfo_
Definition: jobInfo.C:44
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
~sigQuit()
Destructor.
Definition: sigQuit.C:68
errorManip< error > abort(error &err)
Definition: errorManip.H:131
prefixOSstream Perr(cerr, "Perr")
Definition: IOstreams.H:54
sigQuit()
Construct null.
Definition: sigQuit.C:60
Namespace for OpenFOAM.