FreeFOAM The Cross-Platform CFD Toolkit
sigQuitImpl.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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/error.H>
27 #include "sigQuitImpl.H"
28 #include <OpenFOAM/JobInfo.H>
29 #include <OpenFOAM/IOstreams.H>
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 struct sigaction Foam::sigQuitImpl::oldAction_;
34 
35 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
36 
37 void Foam::sigQuitImpl::sigQuitHandler(int)
38 {
39  // Reset old handling
40  if (sigaction(SIGQUIT, &oldAction_, NULL) < 0)
41  {
43  (
44  "Foam::sigQuitImpl::sigQuitHandler()"
45  ) << "Cannot reset SIGQUIT trapping"
46  << abort(FatalError);
47  }
48 
49  // Update jobInfo file
51 
52  error::printStack(Perr);
53 
54  // Throw signal (to old handler)
55  raise(SIGQUIT);
56 }
57 
58 
59 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
60 
62 {
63  oldAction_.sa_handler = NULL;
64 }
65 
66 
67 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
68 
70 {
71  // Reset old handling
72  if (oldAction_.sa_handler && sigaction(SIGQUIT, &oldAction_, NULL) < 0)
73  {
75  (
76  "Foam::sigQuitImpl::~sigQuitImpl()"
77  ) << "Cannot reset SIGQUIT trapping"
78  << abort(FatalError);
79  }
80 }
81 
82 
83 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
84 
85 void Foam::sigQuitImpl::set(const bool verbose)
86 {
87  if (oldAction_.sa_handler)
88  {
90  (
91  "Foam::sigQuitImpl::set()"
92  ) << "Cannot call sigQuitImpl::set() more than once"
93  << abort(FatalError);
94  }
95 
96  struct sigaction newAction;
97  newAction.sa_handler = sigQuitHandler;
98  newAction.sa_flags = SA_NODEFER;
99  sigemptyset(&newAction.sa_mask);
100  if (sigaction(SIGQUIT, &newAction, &oldAction_) < 0)
101  {
103  (
104  "Foam::sigQuitImpl::set()"
105  ) << "Cannot set SIGQUIT trapping"
106  << abort(FatalError);
107  }
108 }
109 
110 
111 // ************************ vim: set sw=4 sts=4 et: ************************ //