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